我正在使用多个地图标记。目前我在我的JavaScript中使用map.fitBounds(bounds);
来调整地图大小。边界包含多个LatLng
对象。Google Maps JavaScript API - 自动缩放级别?
我在做什么错了?因为它缩小太远:-(
JavaScript源
var geocoder, map;
$(document).ready(function(){
var coll_gmap = $(".gmap");
if (coll_gmap.length != 0)
{
//initiate map
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 13,
center: latlng,
mapTypeControl: true,
navigationControl: true,
scaleControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.ZOOM_PAN},
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var bounds = new google.maps.LatLngBounds();
//loop all addressen + insert into map
map = new google.maps.Map(coll_gmap[0], myOptions);
coll_gmap.each(function(index)
{
if (geocoder) {
geocoder.geocode({ 'address': $(this).attr("address")}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
bounds.extend(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
console.log("Geocode was not successful for the following reason: " + status);
}//end if status
}); //end if geocoder.geocode
} //end if geocoder
}) //end coll_gmap each
map.fitBounds(bounds);
}//end if coll_gmap length
/* console.log("Script created by NicoJuicy");*/
}); //end onload
HTML源
<div class="gmap" address="SomeAddress1" style="width:700px;height:350px"></div>
<div class="gmap" address="someAddress2"></div>
尼斯反应。 不过,我想知道,如果你发现我的截图,我的标记并不是在中间,更多的左半部。 所以别的东西必须是错的,另外,变焦时不我调整宽度得到“接近” ... – NicoJuicy 2010-07-15 08:15:40
@NicoJuicy:是否有可能与复制您的问题一个完整的例子来更新你的问题?我试图把标记在相同的位置和你,和'fitBounds()'似乎是工作在我截图好多了。 – 2010-07-15 11:32:11
我已经更新我的源:) – NicoJuicy 2010-07-16 08:45:51