gpt4 book ai didi

Jquery Google Maps V3 - 信息窗口始终固定为一个标记

转载 作者:行者123 更新时间:2023-12-01 04:27:30 25 4
gpt4 key购买 nike

下面是我根据此处给出的示例编写的代码 Google Map V3 Demo

我的问题是点击任何标记总是在一个固定点打开信息窗口我的意思是如果我有4个标记marker1,marker2,marker3,marker4 ..如果我点击mark1或mark2或mark3或mark4信息窗口是总是在marker4处打开以及marker4的信息

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery + Google Maps API v3 Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var map;
var arrMarkers = [];
var arrInfoWindows = [];

function mapInit(){
var centerCoord = new google.maps.LatLng(17.22, 78.29);
var mapOptions = {
zoom: 6,
center: centerCoord,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById("map"), mapOptions);

$.getJSON("mapDB.php", function(mapPoints){
for (var i = 0; i < mapPoints.length; i++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(mapPoints[i].latitude,mapPoints[i].longitude),
map: map,
title: "test"+i
});
arrMarkers[i] = marker;
var infowindow = new google.maps.InfoWindow({
content: "<h3>Test this</h3>"+i
});
arrInfoWindows[i] = infowindow;
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});


}

});
}
$(function(){
mapInit();

});
</script>
<style type="text/css" media="screen">
img { border: 0; }
#map{
width: 500px;
height: 600px;;


}
#content {
position: fixed;
top: 10px;
left: 800px;
margin: 30px;
}
</style>
</head>

<body>
<div id="container">
<div id="map"></div>
</div>

</body>
</html>

感谢您的帮助问候基兰

最佳答案

我认为您对变量存在一些范围界定问题。没有太多时间深入挖掘。我把这个 jsFiddle 放在一起:http://jsfiddle.net/rcravens/T7HwX/2/

这是我更新以使其正常工作的代码:

    var count = mapPoints.length;
for (var i = 0; i < count; i++) {
var latlng = new google.maps.LatLng(mapPoints[i].latitude,mapPoints[i].longitude);
var title = "test" + i;
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: title
});
arrMarkers[i] = marker;
var infowindow = new google.maps.InfoWindow();
arrInfoWindows[i] = infowindow;
marker.myHtml = "<h3>Test this " + i + "</h3>";
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(this.myHtml);
infowindow.open(map, this);
});

}

主要是在创建“addListener”时。

希望这有帮助。

鲍勃

关于Jquery Google Maps V3 - 信息窗口始终固定为一个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5479827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com