简体   繁体   English

将多个标记居中Google API v3

[英]Center multiple markers Google API v3

I realize there are some examples out there of this but I can not make it work with my script. 我意识到这里有一些示例,但是我无法使其与我的脚本一起使用。 I realize you need to make a bounds but it does not want to work. 我知道您需要做一个界限,但是它不想工作。

Here is my code: 这是我的代码:

<script type="text/javascript">
  var infowindow;
  var map;

  function initialize() {
    var myLatlng = new google.maps.LatLng(37.4419, -122.1419);
    var myOptions = {
      zoom: 13,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    downloadUrl("include/moredata.xml", function(data) {
      var markers = data.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        var latlng = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
        var marker = createMarker(markers[i].getAttribute("name"), latlng);
       }
     });
  }

  function createMarker(name, latlng) {
    var marker = new google.maps.Marker({position: latlng, map: map});
    google.maps.event.addListener(marker, "click", function() {
      if (infowindow) infowindow.close();
      infowindow = new google.maps.InfoWindow({content: name});
      infowindow.open(map, marker);
    });
    return marker;
  }

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

</script>

At first I thought it was because I declared a center and zoom in my options, but even after taking those out, my map did not show up. 起初我以为是因为我宣布了一个中心并放大了我的选择,但是即使删除了这些选择,我的地图也没有显示出来。 Help please??? 请帮助??? Thank you! 谢谢!

First set up an array: 首先建立一个数组:

var realMarkers = [];

Then push markers into the array: 然后将标记推入数组:

var marker = createMarker(markers[i].getAttribute("name"), latlng);
realMarkers.push(marker);

Then as you loop realMarkers: 然后,在循环realMarkers时:

bounds.extend(marker.getPosition());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Maps API v3-在一页上创建带有标记的多张地图 - Google Maps API v3 - creating multiple maps with markers on one page Google Maps(api v3) - 循环访问mysql查询结果,以便在地图上进行地理编码和放置多个标记 - Google Maps (api v3) - Loop through mysql query results to geocode and place multiple markers on map Google Maps API v3刷新标记 - Google maps api v3 refreshing away markers Google Maps v3 api中的标记不再访问内容 - Markers in Google maps v3 api no longer access content 用php解析xml,google maps api v3标记 - parse xml with php , google maps api v3 markers 谷歌地图api v3自动中心自动缩放 - google maps api v3 auto center auto zoom 适用于Codeigniter的Google Maps V3 API [添加数字标记和其他问题] - Google Maps V3 API for Codeigniter [Adding Numbered Markers + other questions] 如何使用带有可点击标记的v3 API和标记内的自定义html创建Google Map? - How do I create a Google Map using v3 API with clickable markers with custom html inside the marker? 使用Google地图API API v3的DirectionsService查找并显示最近标记的路线 - Find and show the route of the closest markers using DirectionsService by Google Map API v3 如何使用API​​ V3在页面中显示多个Google Maps - How to Display Multiple Google Maps in the page with API V3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM