适合谷歌地图上的标记

时间:2014-10-29 07:07:22

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

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

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

1 个答案:

答案 0 :(得分:1)

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

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