Why am i not getting exact Longitude when i click on a marker?

In my web app i am trying to fetch longitude and latitude of a marker when i click on it. Following is the code which is setting a marker and then try to get the same marker's latitude and longitude when i click on it:

  var myLatlng = new google.maps.LatLng(55.68322317670628, 13.177157360327598);
  var marker_obj = new google.maps.Marker({
    clickable: true,
    position: myLatlng,
    map: dashboard_map,
    zIndex: i
 });

  marker_obj.setIcon('marker.png');

  google.maps.event.addListener(marker_obj, "click", function(event) {
        var myLatLng = event.latLng;
        var lat1 = myLatLng.lat();
        var lng1 = myLatLng.lng();
        console.log('marker latitude: '+lat1);
        console.log('marker longitude: '+lng1);

        $.ajax({
            type: 'POST',
            url: 'http://localhost:8888/action',
            data: { latitude: lat1, longitude: lng1},
            success: function(result) {
                console.log(result);
            }
        });
    });

When i click on marker i get right latitude but longitude is not same. longitude value is 13.177157360327556, original marker longitude value is 13.177157360327598. Why am i not getting right longitude? Thanks in advance.


Solution 1:

To get the position of the marker (not the click), use marker_obj.getPosition() not event.latLng.

google.maps.event.addListener(marker_obj, "click", function(event) {
    var myLatLng = marker_obj.getPosition();
    var lat1 = myLatLng.lat();
    var lng1 = myLatLng.lng();
    console.log('marker latitude: '+lat1);
    console.log('marker longitude: '+lng1);

    $.ajax({
        type: 'POST',
        url: 'http://localhost:8888/action',
        data: { latitude: lat1, longitude: lng1},
        success: function(result) {
            console.log(result);
        }
    });
});