gpt4 book ai didi

javascript - 将 infoWindow 包含到 gmaps markclusterer 中时出现问题

转载 作者:行者123 更新时间:2023-12-03 02:25:55 24 4
gpt4 key购买 nike

我在将 infoWindow 添加到现有 gmaps markclusterer 项目时遇到问题。我知道有类似的问题,但我看到的答案没有用。可能是因为我对此还很陌生并且错过了一些东西。

下面是我的代码,没有信息窗口。每个标记都是根据 .jason 数据文件中的纬度/经度生成的。“预尝试”代码:

    <script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="data.json"></script>
<script type="text/javascript" src="markerclusterer.js"></script>

<script>
function initialize() {
var center = new google.maps.LatLng(34.777491, 64.5852620);

var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var markers = [];
for (var i = 0; i < 100; i++) {
var dataPhoto = data.photos[i];
var latLng = new google.maps.LatLng(dataPhoto.latitude,
dataPhoto.longitude);
var marker = new google.maps.Marker({
position: latLng,
icon: "images/gps.svg"
});
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers,
{imagePath:'images/m'});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>

<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12846745-20']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type =
'text/javascript'; ga.async = true;
ga.src = ('https:' === document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>

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

尝试但不起作用的代码:

<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="data.json"></script>
<script type="text/javascript" src="markerclusterer.js"></script>

<script>
function initialize() {
var center = new google.maps.LatLng(34.777491, 64.5852620);

var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var markers = [];
for (var i = 0; i < 100; i++) {
var dataPhoto = data.photos[i];
var infoWin = new google.maps.InfoWindow();
var latLng = new google.maps.LatLng(dataPhoto.latitude,
dataPhoto.longitude);
var marker = new google.maps.Marker({
position: latLng,
icon: "images/gps.svg"
});
google.maps.event.addListener(marker, 'click', function(evt) {
infoWin.setContent('content here');
infoWin.open(map, marker);
})
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers, {imagePath:'images/m'});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>

<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12846745-20']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

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

我不知道我是否遗漏了一些明显的东西,或者是否与点击缩放功能有冲突。需要明确的是,我希望在只有一个设备可点击而不是集群时实现 infoWindow。感谢您的帮助,我真的很感激。

更新:

var data = { "count": 10785236,
"photos": [{"photo_id": 27932, "photo_title": "Atardecer en Embalse", "photo_url": "http://www.panoramio.com/photo/27932", "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/27932.jpg", "longitude": -64.404945, "latitude": -32.202924, "width": 500, "height": 375, "upload_date": "25 June 2006", "owner_id": 4483, "owner_name": "Miguel Coranti", "owner_url": "http://www.panoramio.com/user/4483"}

上面显示的 var 数据是 .json 文件中的示例。这是众多条目之一。

最佳答案

enter image description here在您的 for 循环 中,缺少一个函数闭包,因此您无法捕获 infowindow 对象并将它们分配给它们的相对标记,更不用说显示它们的内容了。

更改此:

google.maps.event.addListener(marker, 'click', function(evt) {
infoWin.setContent('content here');
infoWin.open(map, marker);
})

为此:

(function(m, infoWindow, idx){
google.maps.event.addListener(m, 'click', function(evt) {
infoWindow.setContent('marker ' + idx);
infoWindow.open(map, m);

})
})(marker, infoWin, i);

为了重现该问题,我在 JSON 对象中使用了 100 个不同的纬度/经度,以表示 for 循环中的迭代次数。

Get the JSBIN with code sample here.

关于javascript - 将 infoWindow 包含到 gmaps markclusterer 中时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48945517/

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