1
目前我有使用Google地點在Google地圖上顯示標記的JavaScript,但我只是重構我的代碼以減少重複函數並將它們放入數組中。重構代碼後,標記不再顯示。重構的JavaScript不在Google地圖上顯示標記
這是我的代碼。
<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>
我試圖找到問題,但我沒有得到任何地方。 有什麼建議嗎?
我不知道很多關於谷歌地圖開發,但我會嘗試刪除代碼:如果(狀態== ...)在回調()中,看看它是否有幫助。 – mkoistinen 2012-02-18 11:37:51