1

我想在地圖上放置多個標記,但我得到的地圖沒有設置標記。無法使用Google地圖在Google地圖上設置多個標記javascript api v3

<script type="text/javascript"> 

    var map; 
    var i; 

    window.onload=function() 
    { 
    var mapOptions = { 
     zoom: 13, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     center: new google.maps.LatLng(59.32522, 18.07002) 
    }; 

    map = new google.maps.Map(document.getElementById("map"),mapOptions); 
    var bounds = new google.maps.LatLngBounds(); 
     var places =[]; 
     places.push(google.maps.LatLng(40.756,-73.986)); 
     places.push(google.maps.LatLng(59.32522, 18.07002)); 
     places.push(google.maps.LatLng(37.775,-122.419)); 
     places.push(google.maps.LatLng(47.620,-122.347)); 
     places.push(google.maps.LatLng(-22.933,-43.184)); 
     for(i=0; i<places.length;i++) 
     { 
      var marker= new google.maps.Marker({position:places[i],map: map,title:'place Number'+i}); 

     bounds.extend(places[i]); 

     } 
     map.fitBounds(bounds); 
    } 
</script> 

回答

0

你需要改變所有的線條看起來像這樣:

 places.push(google.maps.LatLng(40.756,-73.986)); 

看起來像這樣:

 places.push(new google.maps.LatLng(40.756,-73.986)); 

下面是上面這些改動你的代碼。它爲我工作。試試看:

<script type="text/javascript"> 

    var map; 
    var i; 

    window.onload=function() 
    { 
    var mapOptions = { 
     zoom: 13, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     center: new google.maps.LatLng(59.32522, 18.07002) 
    }; 

    map = new google.maps.Map(document.getElementById("map"),mapOptions); 
    var bounds = new google.maps.LatLngBounds(); 
     var places =[]; 
     places.push(new google.maps.LatLng(40.756,-73.986)); 
     places.push(new google.maps.LatLng(59.32522, 18.07002)); 
     places.push(new google.maps.LatLng(37.775,-122.419)); 
     places.push(new google.maps.LatLng(47.620,-122.347)); 
     places.push(new google.maps.LatLng(-22.933,-43.184)); 
     for(i=0; i<places.length;i++) 
     { 
      var marker= new google.maps.Marker({position:places[i],map: map,title:'place Number'+i}); 

     bounds.extend(places[i]); 

     } 
     map.fitBounds(bounds); 
    } 
</script> 
+0

感謝幫助,但它仍然沒有工作。 – geetika 2011-06-03 09:45:15

+0

我已經明確添加了您的修改後的代碼將看起來像答案。嘗試剪切並粘貼它。 – Trott 2011-06-03 14:02:48

+0

感謝@Trott現在的工作 – geetika 2011-06-03 16:39:44

0

您可以編寫一個JavaScript函數並在填充結果後調用。你也可以使用JSON數據推Serverside集團的JavaScript

function showMap(locations){ 
    //map code here 
} 
相關問題