Get Coordinates from Map Click?

11,837 views
Skip to first unread message

David

unread,
Mar 31, 2011, 3:35:34 PM3/31/11
to Google Maps JavaScript API v3
I'm trying to get the coordinates for a single click on the map.

The code below is something I've cobbled together that gets the coords
for the center of the map, upon double click. But I'd like to get the
point under the mouse when I single click.

(stuff in comments are attempts)


google.maps.event.addListener(
map, "dblclick", function(
overlay, latlng, overlaylatlng
) {
// var point = new GLatLng( latlng.y, latlng.x );
//document.getElementById("log").innerHTML =
map.getCenter().toString();
document.getElementById("log").innerHTML = '|' +
map.getCenter().lat().toFixed(6) + ' |' +
map.getCenter().lng().toFixed(6);
});


same code here...
http://pastebin.com/A293Et9b

Andreas Sommer

unread,
Mar 31, 2011, 4:15:56 PM3/31/11
to google-map...@googlegroups.com
This is mixed V2 and V3 code. See the reference for the Map class, the dblclick event callback only gets one argument (MouseEvent), which contains the lat/lng of the click.

David

unread,
Apr 4, 2011, 3:20:55 PM4/4/11
to Google Maps JavaScript API v3
Thanks! That got me on the right track, and I figured it out!

Here's what I went with...

google.maps.event.addListener(map, "click", function (e) {
document.form1.waypointLog.value = e.latLng.lat().toFixed(6)
+ ' |' + e.latLng.lng().toFixed(6);
});





On Mar 31, 4:15 pm, Andreas Sommer <andreas.somme...@googlemail.com>
wrote:
> On 31.03.2011 21:35, David wrote:
>
> > I'm trying to get the coordinates for a single click on the map.
>
> > The code below is something I've cobbled together that gets the coords
> > for the center of the map, upon double click.  But I'd like to get the
> > point under the mouse when I single click.
>
> > (stuff in comments are attempts)
>
> > google.maps.event.addListener(
> >    map, "dblclick", function(
> >            overlay, latlng, overlaylatlng
> >    ) {
> >            // var point = new GLatLng( latlng.y, latlng.x );
> >            //document.getElementById("log").innerHTML =
> > map.getCenter().toString();
> >            document.getElementById("log").innerHTML = '|' +
> > map.getCenter().lat().toFixed(6) + ' |' +
> > map.getCenter().lng().toFixed(6);
> >    });
>
> This is mixed V2 and V3 code. See the reference for the Map class
> <https://code.google.com/apis/maps/documentation/javascript/reference....>,
Reply all
Reply to author
Forward
0 new messages