Omar Abid
Omar Abid

Reputation: 15966

Getting Google Maps Lat and Long coordinates

I'm using the following code to get the coordinates of my Google maps marker as it's position is changed

    google.maps.event.addListener(marker, 'drag', function() {
        lat = Math.round(marker.position.Ma * 1000) / 1000;
        lng = Math.round(marker.position.Na * 1000) / 1000;
        [.. using lat and long]
    });

This code used to work fine. From time to time, Google changed the position variables name Ma and Na (not sure why they do so).

So I'm using the right variables to get the lat and long, or is there another way of doing it?

Upvotes: 0

Views: 1270

Answers (2)

U.Ahmad
U.Ahmad

Reputation: 618

I have used the following the drag event gives a MouseEvent object with latLng:

google.maps.event.addListener(marker, 'drag', function(event) {
    lat = Math.round(event.latLng.lat() * 1000) / 1000;
    lng = Math.round(event.latLng.lng() * 1000) / 1000;
    [.. using lat and long]
});

Upvotes: 1

Tim
Tim

Reputation: 14446

Try this:

lat = Math.round(1000 * marker.getPosition().lat()) / 1000;
lng = Math.round(1000 * marker.getPosition().lng()) / 1000;

It sounds like you're using their private variables instead of accessor methods.

Upvotes: 2

Related Questions