2013-03-20 45 views
8

我需要一些幫助來在Google地圖中創建標記數組,以便它可以更高效,而不是逐個創建標記。我嘗試了以下,但它不起作用。任何人都有建議?在Google地圖中添加標記陣列

//create array to store a set of location 
var collection = new Array(); 

//a set of locations stored in array 
collection[0] = new google.maps.LatLng(13.742167701649997, 100.50721049308777); 
collection[1] = new google.maps.LatLng(13.74428, 100.5404525); 
collection[2] = new google.maps.LatLng(13.744108, 100.543098); 

var pointMarkerImage = new Array();//store image of marker in array 
var pointMarker = new Array();//store marker in array 

//create number of markers based on collection.length 
function setPoint(){ 
for(var i=0; i<collection.length; i++){ 

var pointMarkerImage[i] = new google.maps.MarkerImage('marker.png'); 
var pointMarker[i] = new google.maps.Marker({ 
     position: collection[i], 
     map: map, 
     icon: pointMarkerImage[i], 
     animation: google.maps.Animation.BOUNCE, 
     title: "collection"+ i 
    }); 

    google.maps.event.addListener(pointMarker[i], 'click', function() { 
      window.open("blog/page01.html","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=yes") 
      }; 
    ); 
    } 
} 
+0

你確定你在調用'setPoint()'嗎?你在哪裏調用這個函數? – 2013-03-20 18:10:26

+0

你沒有想到for循環,你不需要爲每個數組索引創建一個新的latlng對象,你可以存儲這個數字並且做(for tipical){new google.map。(lat [i]) }類似 – 2013-03-20 18:10:44

+0

感謝您的意見,我可能需要發佈整個代碼才能給出正確的外觀。 – user2192094 2013-03-21 13:05:00

回答

3

我看到幾件事情:

1)在for循環,VAR pointM ......應該只是pointM ...添加VAR使得它不理你有外面的for循環集。

2)你有一個;之後的一個功能就是打破事情。

試試這個:

//create array to store a set of location 
var collection = new Array(); 

//a set of locations stored in array 
collection[0] = new google.maps.LatLng(13.742167701649997, 100.50721049308777); 
collection[1] = new google.maps.LatLng(13.74428, 100.5404525); 
collection[2] = new google.maps.LatLng(13.744108, 100.543098); 

var pointMarkerImage = new Array();//store image of marker in array 
var pointMarker = new Array();//store marker in array 

//create number of markers based on collection.length 
function setPoint(){ 
    for(var i=0; i<collection.length; i++){ 

    pointMarkerImage[i] = new google.maps.MarkerImage('marker.png'); 
    pointMarker[i] = new google.maps.Marker({ 
      position: collection[i], 
      map: map, 
      icon: pointMarkerImage[i], 
      animation: google.maps.Animation.BOUNCE, 
      title: "collection"+ i 
    }); 

    google.maps.event.addListener(pointMarker[i], 'click', function(){ 
     window.open("blog/page01.html","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=yes"); 
    } 
    ); 
    } 
} 
+0

感謝您的反饋。這是一個粗心的錯誤!不知道爲什麼它仍然無法正常工作。我可能想要發佈下面的整個代碼以提供更好的圖片。 – user2192094 2013-03-21 13:06:16

25

這是我簡單的代碼,它工作正常。當你點擊標記時,它將根據標記的位置分別打開信息窗口。

<script> 
var locations = [ 
    ['Title A', 3.180967,101.715546, 1], 
    ['Title B', 3.200848,101.616669, 2], 
    ['Title C', 3.147372,101.597443, 3], 
    ['Title D', 3.19125,101.710052, 4] 
]; 
var map = new google.maps.Map(document.getElementById('map'), { 
    zoom: 12, 
    center: new google.maps.LatLng(3.171368,101.653404), 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}); 

var infowindow = new google.maps.InfoWindow; 

var marker, i; 

for (i = 0; i < locations.length; i++) { 
    marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
     map: map 
    }); 

    google.maps.event.addListener(marker, 'click', (function(marker, i) { 
     return function() { 
      infowindow.setContent(locations[i][0]); 
      infowindow.open(map, marker); 
     } 
    })(marker, i)); 
} 
</script> 

<div data-role="page" id="map_result"> 
    <div data-role="header" data-position="fixed"><h1>Multiple Marker</h1></div> 
    <div data-role="content" style="padding:0;"> 
     <div id="map"></div> 
    </div> 
</div> 
+1

這個函數(i)返回函數的東西非常漂亮。 – 2015-02-27 07:05:36

相關問題