gpt4 book ai didi

javascript - 一次放下一个标记

转载 作者:行者123 更新时间:2023-11-28 19:39:50 26 4
gpt4 key购买 nike

我正在使用 Google Maps Api v3,并且尝试在 map 上一次放置一个标记,就像 Google Demo 一样但我无法让它工作,这是我的代码,所有标记都会同时删除。

var map;
var markers = [];

function initialize() {
var latlng = new google.maps.LatLng(52.520816, 13.410186);

var options = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

map = new google.maps.Map(document.getElementById("map-canvas"), options);
}

initialize();

function loadMarkers() {

$.ajax({
url: 'js/poi.php',
type: 'GET',
dataType : "json",
success: function (data) {

var latlngbounds = new google.maps.LatLngBounds();


$.each(data, function(index, point) {

var marker = new google.maps.Marker({
position: new google.maps.LatLng(point.Lat, point.Lng),
animation: google.maps.Animation.DROP,
icon: 'img/marker.png'

});


markers.push(marker);
latlngbounds.extend(marker.position);

});

var markerCluster = new MarkerClusterer(map, markers);
map.fitBounds(latlngbounds);

}

});
}

loadMarkers();

我尝试在每个标记上使用超时,但我猜想 loadMarkers();总是会立即丢弃它们...

    setTimeout(function() {

var marker = new google.maps.Marker({
position: new google.maps.LatLng(point.Lat, point.Lng),
animation: google.maps.Animation.DROP,
icon: 'img/marker.png'

});

}, point.Id * 200);

关于如何解决这个问题有什么想法吗?

编辑: poi.php 文件列出了我的表中的所有点并像这样输出它们:

[
{"Id":"1","Lat":"52.511467","Lgn":"13.447179"},
{"Id":"2","Lat":"52.549061","Lgn":"13.422975"},
{"Id":"3","Lat":"52.497622","Lgn":"13.396110"},
{"Id":"4","Lat":"52.517683","Lgn":"13.394393"}
]

最佳答案

  1. 在将标记添加到 map 时将其添加到聚类器
  2. 调整边界以显示添加的标记
  3. 修复了 JSON 中的拼写错误(不知道这是否是一个问题)
function initialize() {
var latlng = new google.maps.LatLng(52.520816, 13.410186);

var options = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

map = new google.maps.Map(document.getElementById("map-canvas"), options);
loadMarkers();
}

function loadMarkers() {
$.ajax({
type: 'POST',
dataType: 'json',
url: '/echo/json/',
data: {
json: JSON.stringify(jsonData)
},
delay: 3,
success: function (data) {
var markerCluster = new MarkerClusterer(map, markers);
var latlngbounds = new google.maps.LatLngBounds();
$.each(data, function (index, point) {
setTimeout(function() {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(point.Lat, point.Lng),
animation: google.maps.Animation.DROP,
map: map /* don't have your custom marker
icon: 'img/marker.png'*/
});
markerCluster.addMarker(marker);
markers.push(marker);
// adjust the bounds to show all the markers
latlngbounds.extend(marker.getPosition());
map.fitBounds(latlngbounds);
}, point.Id * 200);
});
}
});
}

working fiddle

关于javascript - 一次放下一个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25307679/

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