问题描述
在我的网络应用程序中,当我点击一个标记时,我试图获取它的经度和纬度.以下是设置标记然后尝试在我单击时获取相同标记的纬度和经度的代码:
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);
}
});
});
当我点击标记时,我得到正确的纬度,但经度不一样.经度值为 13.177157360327556,原始标记经度值为 13.177157360327598.为什么我没有得到正确的经度?提前致谢.
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.
推荐答案
要获取标记的位置(不是点击),请使用 marker_obj.getPosition() 而不是 event.latLng.
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);
}
});
});
这篇关于为什么我点击标记时没有得到准确的经度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!