我正在处理一个涉及 Google Maps API 和 infowindows 的问题。当我单击一个标记时,会弹出一个信息窗口,我正在尝试制作它,以便在发生这种情况时,该标记的纬度/经度会显示在信息窗口中。在这种情况下,lat/lng 存储在 stop 数组中:
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
var map;
var marker;
var infowindow; //Global infowindow created
function initialize() {
var latlng = new google.maps.LatLng(37.784, -122.408);
var stops = new Array();
stops = [
new google.maps.LatLng(37.7655, -122.4525899),
new google.maps.LatLng(37.7649999, -122.45656),
new google.maps.LatLng(37.7643, -122.4608199),
];
var myOptions = {
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(37.7655, -122.4525899)
};
map = new google.maps.Map(document.getElementById("content"),
myOptions);
infowindow = new google.maps.InfoWindow({ //infowindow options set
maxWidth: 355
});
var i = 0;
for (i < 0; i < stops.length; i++) {
marker = new google.maps.Marker({
position:stops[i],
map: map,
title: "Stop Coords: "+ stops[i]
});
var len=stops.length;
popupDirections(marker, len, stops);
}
}
function popupDirections(marker, len,stops) {
//this function created listener listens for click on a marker
google.maps.event.addListener(marker, 'click', function () {
for (var i = 0; i < len; i++)
{ infowindow.setContent("Stop coords: " + stops[i]); } //sets the content of your global infowindow to string "Tests: "
infowindow.open(map, marker); //then opens the infowindow at the marker
});
}
现在,当我将鼠标悬停在每个标记上时,会显示正确的纬度/经度,但是当我单击标记并弹出信息窗口时,我会不断获得停靠点的纬度/经度 [2]。我不确定问题是什么,我对 JS 的了解非常有限,我很幸运在这里找到了一个有用的帖子,展示了如何创建 infowindows。