Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture Coordinates in Google Map on User Click

I'm using this code to capture the co-ordinates when user clicks on the map by using below event listener:

google.maps.event.addListener(map, 'click', function(event) {     placeMarker(event.latLng); }); 

However this function doesn't get called when user click on already marked location in Map. Meaning this function is not called for points where mouse pointer changes to hand icon on Google Map.

Need help on capturing these kind of locations.

like image 658
Bads123 Avatar asked Sep 30 '13 05:09

Bads123


People also ask

Can you input coordinates into Google Maps?

Go to maps.google.com. 2. Type out the coordinates into the search bar — using either the degrees, minutes, and seconds (DMS) format, the degrees and decimal minutes (DMM) format, or decimal degrees (DD) format — then hit enter or click on the search icon.


2 Answers

You should add the click listener on marker will give you the position of marker.

//Add listener google.maps.event.addListener(marker, "click", function (event) {     var latitude = event.latLng.lat();     var longitude = event.latLng.lng();     console.log( latitude + ', ' + longitude ); }); //end addListener 

Edit: You need something like this

//Add listener google.maps.event.addListener(marker, "click", function (event) {     var latitude = event.latLng.lat();     var longitude = event.latLng.lng();     console.log( latitude + ', ' + longitude );      radius = new google.maps.Circle({map: map,         radius: 100,         center: event.latLng,         fillColor: '#777',         fillOpacity: 0.1,         strokeColor: '#AA0000',         strokeOpacity: 0.8,         strokeWeight: 2,         draggable: true,    // Dragable         editable: true      // Resizable     });      // Center of map     map.panTo(new google.maps.LatLng(latitude,longitude));  }); //end addListener 
like image 119
Ashwin Parmar Avatar answered Oct 02 '22 20:10

Ashwin Parmar


Another solution is to place a polygon over the map, same size as the map rectangle, and collect this rectangles clicks.

function initialize() {   var mapDiv = document.getElementById('map-canvas');   var map = new google.maps.Map(mapDiv, {     center: new google.maps.LatLng(37.4419, -122.1419),     zoom: 13,     mapTypeId: google.maps.MapTypeId.ROADMAP   });    google.maps.event.addListener(map, 'bounds_changed', function() {       var lat1 = 37.41463623043073;       var lat2 = 37.46915383933881;       var lng1 = -122.1848153442383;       var lng2 = -122.09898465576174;          var rectangle = new google.maps.Polygon({          paths : [            new google.maps.LatLng(lat1, lng1),            new google.maps.LatLng(lat2, lng1),            new google.maps.LatLng(lat2, lng2),            new google.maps.LatLng(lat1, lng2)          ],         strokeOpacity: 0,         fillOpacity : 0,         map : map       });       google.maps.event.addListener(rectangle, 'click', function(args) {            console.log('latlng', args.latLng);     });   }); } 

Now you get LatLng's for places of interest (and their likes) also.

demo -> http://jsfiddle.net/qmhku4dh/

like image 35
davidkonrad Avatar answered Oct 02 '22 18:10

davidkonrad