2014-12-25 62 views
-1

我想在自定义地图上显示100个标记,我应该在单个标记代码下面重复100次,并且有大量文件,或者有其他更紧凑的方法来创建多个标记吗?如何在Google地图上创建多个标记100+

var marker = new google.maps.Marker({ 
       position: new google.maps.LatLng(34.052661,-118.269976), 
       title:"Hello World!", 
       map:map, 
       animation: google.maps.Animation.DROP 
      }); 

      google.maps.event.addListener(marker, 'click', function() { 
       infowindow.open(map,marker); 
      }); 

      var infowindow = new google.maps.InfoWindow({ 
       content: '<div id="content">'+ 
           '<div id="siteNotice">'+ 
           '</div>'+ 
           '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+ 
           '<div id="bodyContent">'+ 
           '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' + 
           'sandstone rock formation in the southern part of the '+ 
           'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '+ 
           'south west of the nearest large town, Alice Springs; 450&#160;km '+ 
           '(280&#160;mi) by road. Kata Tjuta and Uluru are the two major '+ 
           'features of the Uluru - Kata Tjuta National Park. Uluru is '+ 
           'sacred to the Pitjantjatjara and Yankunytjatjara, the '+ 
           'Aboriginal people of the area. It has many springs, waterholes, '+ 
           'rock caves and ancient paintings. Uluru is listed as a World '+ 
           'Heritage Site.</p>'+ 
           '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+ 
           'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+ 
           '(last visited June 22, 2009).</p>'+ 
           '</div>'+ 
          '</div>', 
      }); 

回答

0

您可以创建你需要为你的标记,并在循环遍历所有这些数据对象的数组,因此您的代码将更加紧凑和高效。

例如:

var myMarkers = new Array(
      {lat:34.052661, lng:-118.269976, title:"Hello World"}, 
      {lat:14.052661, lng:-108.269976, title:"Hello World 2"}, 
      {lat:14.052661, lng:-108.269976, title:"Hello World 3"}, //Continue... 
    ); 

//LOOP on marker data array 
for(var i = 0; i < myMarkers.length; i++){ 
    var currMarker = myMarker[i]; 
    var marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(currMarker.lat,currMarker.lng), 
      title:currMarker.title, 
      map:map, 
      animation: google.maps.Animation.DROP 
     }); 

     google.maps.event.addListener(marker, 'click', function() { 
      infowindow.open(map,marker); 
     }); 

     var infowindow = new google.maps.InfoWindow({ 
      content: '<div id="content">'+ 
          '<div id="siteNotice">'+ 
          '</div>'+ 
          '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+ 
          '<div id="bodyContent">'+ 
          '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' + 
          'sandstone rock formation in the southern part of the '+ 
          'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '+ 
          'south west of the nearest large town, Alice Springs; 450&#160;km '+ 
          '(280&#160;mi) by road. Kata Tjuta and Uluru are the two major '+ 
          'features of the Uluru - Kata Tjuta National Park. Uluru is '+ 
          'sacred to the Pitjantjatjara and Yankunytjatjara, the '+ 
          'Aboriginal people of the area. It has many springs, waterholes, '+ 
          'rock caves and ancient paintings. Uluru is listed as a World '+ 
          'Heritage Site.</p>'+ 
          '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+ 
          'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+ 
          '(last visited June 22, 2009).</p>'+ 
          '</div>'+ 
         '</div>', 
     }); 
} 
+0

我是保持客户端下载更小的更值得关注,认为谷歌有更有效的方式来增加航点 –

+0

你可以试试库多标记与甚至1000个标记快速度似乎作品: https://code.google.com/p/multimarker/ – cesare

+0

multimarker似乎是一个被遗弃的项目,只是fyi –

相关问题