gpt4 book ai didi

javascript - Bound_Changed 仅在最后添加的标记 Google map v3 时触发

转载 作者:行者123 更新时间:2023-11-28 08:45:15 25 4
gpt4 key购买 nike

我遇到问题,因为当我更改 Google map 的边界时,该事件仅适用于最后插入的标记,并且会删除所有标记详细信息列表。我想要的只是当标记超出谷歌地图上的范围时,列表中该标记的详细信息也会被删除。

我没有任何错误,但我希望有人可以帮助我:)

这是我的脚本:

<script type="text/javascript">
var geocoder;
var map;
enter code here
function initialize() {
var minZoomLevel = 4;
var zooms = 7;
geocoder = new google.maps.Geocoder();

map = new google.maps.Map(document.getElementById('map'), {
zoom: minZoomLevel,
center: new google.maps.LatLng(38.50, -90.50),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

// Bounds for North America
var strictBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(15.70, -160.50),
new google.maps.LatLng(68.85, -55.90)
);

// Listen for the dragend event
google.maps.event.addListener(map, 'dragend', function () {
if (strictBounds.contains(map.getCenter())) return;

// We're out of bounds - Move the map back within the bounds

var c = map.getCenter(),
x = c.lng(),
y = c.lat(),
maxX = strictBounds.getNorthEast().lng(),
maxY = strictBounds.getNorthEast().lat(),
minX = strictBounds.getSouthWest().lng(),
minY = strictBounds.getSouthWest().lat();

if (x < minX) x = minX;
if (x > maxX) x = maxX;
if (y < minY) y = minY;
if (y > maxY) y = maxY;

map.setCenter(new google.maps.LatLng(y, x));
});


// Limit the zoom level
google.maps.event.addListener(map, 'zoom_changed', function () {
if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel);
});

codeAddress();

}
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
function codeAddress() {
var infowindow = new google.maps.InfoWindow();
$.getJSON('/Dashboard/LoadWorkerList', function (address) {
$.each(address, function () {
var currVal = this["AddressLine1"];
geocoder.geocode({ 'address': currVal }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
icon: iconBase + 'man.png',
position: results[0].geometry.location,
title: currVal


})



google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(currVal);
infowindow.open(map, marker);
}
})(marker, currVal));
address.push(marker);

google.maps.event.addListener(map, 'bounds_changed', function () {
$('#places li').css('display', function () {
return (map.getBounds().contains($(this).data('location')))
? ''
: 'none';
});
});
$('#places').append($('<li/>')
.text(currVal)
.data('location', results[0].geometry.location));
enter code here

}
else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
setTimeout(codeAddress, 2000);
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});
});
});
google.maps.event.trigger(map, 'bounds_changed');
}

window.onload = function () {
initialize();

}
</script>

这是我的 Google map 的屏幕截图,其列表如下:

enter image description here

最佳答案

所以在我看来问题是这样的。您正在为每个标记重新定义 map 对象上的 bounds_changed 事件监听器。

相反,当边界发生变化时,您应该只有一个事件监听器,该事件监听器会循环遍历所有标记并决定是否显示/隐藏每个标记。

当您创建标记时,我会将标记粘贴到数组中,然后您可以轻松地在bounds_changed 事件监听器中循环。

关于javascript - Bound_Changed 仅在最后添加的标记 Google map v3 时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19897997/

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