Google maps 如何在Google Maps API v3中设置点数组的中心
我有一张上面有多个位置的地图。位置在运行时根据显示的页面确定。它们是我们经销商的位置,因此根据经销商id的不同而有所不同。标记显示良好,但现在我想将地图居中,以便缩放以包括所有标记,并基于显示的标记居中。我该怎么做?现在,缩放和中心是手动设置的。以下是我目前的代码:Google maps 如何在Google Maps API v3中设置点数组的中心,google-maps,Google Maps,我有一张上面有多个位置的地图。位置在运行时根据显示的页面确定。它们是我们经销商的位置,因此根据经销商id的不同而有所不同。标记显示良好,但现在我想将地图居中,以便缩放以包括所有标记,并基于显示的标记居中。我该怎么做?现在,缩放和中心是手动设置的。以下是我目前的代码: <script type="text/javascript"> function initialize() { var locations = [ <?php
<script type="text/javascript">
function initialize() {
var locations = [
<?php
$loc = '';
$i = 1;
foreach ($coordinates as $c) {
$loc .= '["' . $c->company . '", ' . $c->latitude . ', ' . $c->longitude . '], ';
$i++;
}
echo substr($loc, 0, -2);
?>
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
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
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
创建包含所有标记位置的对象,然后使用对象的fitBounds方法:
创建包含所有标记位置的对象,然后使用对象的fitBounds方法:
我在Firebug中得到了这个错误:属性1,180,-1,-180的值无效。请确保所有标记的位置都是有效的LatLngBounds对象,边界是有效的LatLngBounds对象,有两个Latlngs对象东北、西南GPS坐标。Hummel:您需要在地图中指定一个中心。尽管这是任意的,Google Maps仍然需要它来工作:我在Firebug中得到了这个错误:属性的无效值:1180,-1,-180确保所有标记的位置都是有效的LatLng对象,边界是有效的LatLngBounds对象,有2个LatLng对象,西南GPS坐标:你需要在地图上指定一个中心。尽管这是任意的,谷歌地图仍然需要它来工作:
var bounds = new google.maps.LatLngBounds();
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
});
//Extends bounds to contain marker position
bounds.extend( marker.getPosition() );
.....
}
map.fitBounds( bounds );