首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Maps API V3管理多个标记

Google Maps API V3管理多个标记
EN

Stack Overflow用户
提问于 2014-07-09 17:46:02
回答 1查看 462关注 0票数 0

我正在使用谷歌地图应用程序接口V3的位置参考页面工作,将有大约30个标记。我不确定将托管的html页面上的所有标记是否都是一个好主意,或者它是否应该存储在不同的位置。还包括标记信息(描述)。

我已经使用Google Maps API V3找到了多个标记的代码,并使用谷歌自己的参考资料对其进行了修改。代码如下:

代码语言:javascript
复制
  <script type="text/javascript">

    var infoString = '<div id="content">'+
      '<div id="siteNotice">'+
      '</div>'+
      '<h1 id="firstHeading" class="firstHeading">Auckland 6625</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>'


    var locations = [
      [infoString, -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-39.92, 151.25),
      mapTypeId: google.maps.MapTypeId.TERRAIN
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;
    var markers = new Array();

    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
      });

      markers.push(marker);

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

    function AutoCenter() {
      //  Create a new viewpoint bound
      var bounds = new google.maps.LatLngBounds();
      //  Go through each...
      $.each(markers, function (index, marker) {
      bounds.extend(marker.position);
      });
      //  Fit these bounds to the map
      map.fitBounds(bounds);
    }
    AutoCenter();

  </script> 

由于标记信息(描述)会很长(不像上面显示的那样长),我正在考虑为每个标记创建一个变量。

所以我不确定这是否是最有效的方法。请帮帮忙。

EN

回答 1

Stack Overflow用户

发布于 2014-07-09 18:16:26

你现在做的方式并不是低效的。尽管将每个描述作为单独的变量来使用并没有什么真正的好处,您也可以从一开始就将其作为locations变量的一部分。除非您从其他地方(例如ajax请求)获取数据,然后可以将其添加到location中。

我所做的唯一真正的改变是,我将不再使用二维数组,而是将其设置为一维对象数组。

代码语言:javascript
复制
var locations = [
  {
    description: infoString, 
    lat: -33.890542, 
    lng: 151.274856, 
    foo: 4
  },
  {description: 'Coogee Beach', lat: -33.923036, lng: 151.259052, foo: 5},
  {description: 'Cronulla Beach', lat: -34.028249, lng: 151.157507, foo: 3},
  {description: 'Manly Beach', lat: -33.80010128657071, lng: 151.28747820854187, foo: 2},
  {description: 'Maroubra Beach', lat: -33.950198, lng: 151.259302, foo: 1}
];

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

  markers.push(marker);

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i].description);
      infowindow.open(map, marker);
    }
  })(marker, i));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24650351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档