【问题标题】:Fit markers on google maps适合谷歌地图上的标记
【发布时间】:2014-12-24 19:30:34
【问题描述】:

我一直在尝试将我的标记放在谷歌地图上。 我一直在尝试使用以下内容:

map.fitBounds(latlngbounds); 

我不知道如何实现这一点。怎样才能轻松添加?

到目前为止,我有以下代码:

 <script type="text/javascript">

var locations = <?php echo json_encode($js_array);?>;
var lats = <?php echo json_encode($lats);?>;
var lats = <?php echo json_encode($iphoto);?>;

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 10,
  center: new google.maps.LatLng(<?php echo $latitude;?>, <?php echo $longitude;?>),
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

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

var marker, i;

for (i = 0; i < locations.length; i++) {  
console.log(locations[i].lat);
    marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i].latitude, locations[i].longitude),
    map: map,
    animation: google.maps.Animation.DROP,
    icon: {size: new google.maps.Size(30, 30),
    scaledSize: new google.maps.Size(30, 30),
    url: locations[i].iprofilephoto,}
  });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent("<div><img style='width:40px;height:40px' src='"+locations[i].iprofilephoto+"'>"+locations[i].name+"</div><div><img style='width:100px;height:100px' src='"+locations[i].iphoto+"'></div>");
      infowindow.open(map, marker);
    }
  })(marker, i));
}

【问题讨论】:

标签: javascript php google-maps google-maps-api-3 google-maps-markers


【解决方案1】:

您需要使用所有纬度来扩展边界,如下所示。

var latlngbound = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {  
   // CREATE LATLNG OBJECT AND USE IT TO EXTEND THE LATLNGBOUNDS
   var latlng = new google.maps.LatLng(locations[i].latitude, locations[i].longitude);
   latlngbound .extend(latlong);

   marker = new google.maps.Marker({
   position: latlng,
   map: map,
   animation: google.maps.Animation.DROP,
   icon: {size: new google.maps.Size(30, 30),
   scaledSize: new google.maps.Size(30, 30),
   url: locations[i].iprofilephoto,}
});

最后,您需要调整地图的边界。

map.fitBounds(latlngbounds); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多