我需要一些帮助 我从数据库加载了一些点,并将它们作为标记显示在Google地图上 我的代码:
function DisplayTraficCir(points) {
for (i=0;i<points.length;i++){
var myLatLng = new google.maps.LatLng(points[i].latitude,points[i].longitude)
var marker = new google.maps.Marker({
position: myLatLng,
map: map
});
}
}
现在,当我点击Google地图中的其中一个标记来执行某些操作时,我想获取点的信息(纬度/经度)。
请帮忙。
感谢。
答案 0 :(得分:0)
使用google.maps.event.addListener
方法:
function DisplayTraficCir(points) {
for (i=0;i<points.length;i++){
var myLatLng = new google.maps.LatLng(points[i].latitude,points[i].longitude)
var marker = new google.maps.Marker({
position: myLatLng,
map: map
});
google.maps.event.addListener(marker, 'click', function () {
var lng = points[i].longitue,
lat = points[i].latitude;
alert( lat + "-" + lng );
});
}
}
答案 1 :(得分:0)
虽然razzak的方法可行,但我想建议一个替代方案:
function DisplayTraficCir( points ) {
for( var i = 0; i < points.length; i++ ) {
DisplayTraficPoint( points[i] );
}
}
function DisplayTraficPoint( point ) {
var myLatLng = new google.maps.LatLng( point.latitude, point.longitude );
var marker = new google.maps.Marker({
position: myLatLng,
map: map
});
google.maps.event.addListener( marker, 'click', function() {
alert( point.latitude + "-" + point.longitude );
});
}
通过简单地将循环体移动到它自己的函数中,代码现在每次调用函数时都会唯一地捕获该函数内的变量。也就是说,point
变量指的是调用它的特定points[i]
值。同样,myLatLng
和marker
在每次调用此函数时都是唯一的。
这为您提供了一种非常灵活和强大的方法来跟踪您可能与标记关联的任何其他数据:只需将该数据作为DisplayTraficPoint()
内的局部变量。
答案 2 :(得分:0)
单击标记时,只需调用graphAddress()函数。
function graphAddress(){
geocoder.geocode({'latLng': marker_wizard.getPosition()}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var address = results[0].formatted_address.split(",");
var country = address[address.length -1];
var cityname = address[address.length -3];
var state_zipcode = address[address.length -2].split(" ");
var state = state_zipcode[1];
var zipcode = state_zipcode[2];
address = address.splice(0,(address.length -3));
}
}
});
} //graphAddress
注意: marker_wizard是标记的变量名。