gpt4 book ai didi

javascript - 重构的 JavaScript 在谷歌地图上不显示标记

转载 作者:行者123 更新时间:2023-11-30 18:32:12 24 4
gpt4 key购买 nike

目前我有 JavaScript 可以使用 Google 地点在 Google map 上显示标记,但我只是重构了我的代码以减少重复函数并将它们放入数组中。重构代码后,标记不再显示。

这是我的代码。

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?    sensor=true&libraries=places"></script>
<script type="text/javascript">

var map = [], infoWindow = [];

function initialize(){
initMap(0, 51.5069999695, -0.142489999533);
initMap(1, 40.79445,-74.01558);
initMap(2 , 48.858001709, 2.29460000992);
}

function initMap(ndx, lat, lng){
var feed = new google.maps.LatLng(lat, lng);

map[ndx] = new google.maps.Map(document.getElementById('map'+ndx), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: feed,
zoom: 11
});

var request = {
location: feed,
radius: 5000
};

infoWindow[ndx] = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.search(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(0, results[i]);
createMarker(1, results[i]);
createMarker(2, results[i]);
}
}
}

function createMarker(ndx, place) { =
var marker = new google.maps.Marker({
animation: google.maps.Animation.DROP,
map: map[ndx],
position: place.geometry.location
});

google.maps.event.addListener(marker, 'click', function() {
infowindow[ndx].setContent(place.name);
infowindow[ndx].open(map[ndx], this);
});
}

google.maps.event.addDomListener(window, 'load', initialize);

</script>

<div id="map0" style="position:absolute; width: 290px; height: 300px;"></div>

<div id="map1" style="position:absolute; left:490px; width: 290px; height: 300px;"></div>

<div id="map2" style="position:absolute; left:790px; width: 290px; height: 300px;"></div>

我已尝试找出问题所在,但一无所获。有什么建议吗?

最佳答案

不应该

var service = new google.maps.places.PlacesService(map);

成为

var service = new google.maps.places.PlacesService(map[ndx]);

关于javascript - 重构的 JavaScript 在谷歌地图上不显示标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9340678/

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