Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add marker to Google Map on Click

I'm surprisingly struggling to find a very simple example of how to add a marker(s) to a Google Map when a user left clicks on the map.

I have looked around for the past couple of hours, and consulted the Google Maps API documentation, and would appreciate some help!

like image 366
Andre R. Avatar asked Apr 03 '13 16:04

Andre R.


People also ask

Can you drop markers on Google Maps?

To drop a pin on Google Maps when using an Android device: Open the Google Maps app. Either search for an address or scroll around the map until you find the location you want. Long-press on the screen to drop a pin.


2 Answers

After much further research, i managed to find a solution.

google.maps.event.addListener(map, 'click', function(event) {    placeMarker(event.latLng); });  function placeMarker(location) {     var marker = new google.maps.Marker({         position: location,          map: map     }); } 
like image 62
Andre R. Avatar answered Sep 25 '22 11:09

Andre R.


In 2017, the solution is:

map.addListener('click', function(e) {     placeMarker(e.latLng, map); });  function placeMarker(position, map) {     var marker = new google.maps.Marker({         position: position,         map: map     });     map.panTo(position); } 
like image 45
David Corral Avatar answered Sep 21 '22 11:09

David Corral