简体   繁体   English

如何创建缩放适合谷歌地图标记?

[英]How to create zoom fit google map marker?

i am had some trouble in google map marker fit Zoom.i am get the latlng data dynamically in Xml format. 我在谷歌地图标记适合Zoom.i有一些麻烦我以Xml格式动态获取latlng数据。

here this my code.i need some help pls any one help. 这里我的代码。我需要一些帮助请任何一个帮助。

var markers = xml.documentElement.getElementsByTagName('marker');
        Array.prototype.forEach.call(markers, function(markerElem) {
          var id = markerElem.getAttribute('id');
          var name = markerElem.getAttribute('name');
          var address = markerElem.getAttribute('address');
          var type = markerElem.getAttribute('user_type');
          var point = new google.maps.LatLng(
              parseFloat(markerElem.getAttribute('lat')),
              parseFloat(markerElem.getAttribute('lng')));


            // var res = point.split({lat: lng:});

          var infowincontent = document.createElement('div');
          var strong = document.createElement('strong');
          strong.textContent = name
          infowincontent.appendChild(strong);
          infowincontent.appendChild(document.createElement('br'));

          var text = document.createElement('text');


          text.textContent = address//address view
          infowincontent.appendChild(text);
          var icon = customLabel[type] || {};
             var icons = ["http://maps.google.com/mapfiles/ms/icons/green.png","http://maps.google.com/mapfiles/ms/icons/blue.png",
                        "http://maps.google.com/mapfiles/ms/icons/yellow.png"
                         ];



            var marker = new google.maps.Marker({
            map: map,
            //icon:icons[i],
            icon:icons[Math.floor(Math.random()*icons .length)],
            position: point,
            label: icon.label
          });

You can use fitBounds() method: 您可以使用fitBounds()方法:

var bounds = new google.maps.LatLngBounds();

//extend the bounds for each marker that you add
bounds.extend(marker1.getPosition());
bounds.extend(marker2.getPosition());

//update the map to fit the bounds
map.fitBounds(bounds);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM