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 
}