2011-05-13 51 views
2

我正在使用jQuery的gMap插件。在我的地图上,我有几个标记。我如何根据标记的范围将地图居中?那么,如果可能的话,稍微缩小以在最外面的标记和地图的边缘之间留出边距?如何使用gMap将具有多个标记的地图居中?

谢谢!

编辑1中的代码类似于从http://gmap.nurtext.de/examples.html这个例子...

$("#map4").gMap({ markers: [{ latitude: 47.651968, 
           longitude: 9.478485, 
           html: "_latlng" }, 
          { address: "Tettnang, Germany", 
           html: "The place I live" }, 
          { address: "Langenargen, Germany", 
           html: "_address" }], 
        address: "Braitenrain, Germany", 
        zoom: 10 }); 

回答

9

这可以帮助你一点点......

//Zoom and center the map to fit the markers 
//This logic could be conbined with the marker creation. 
//Just keeping it separate for code clarity. 
var bounds = new google.maps.LatLngBounds(); 
for (index in markers) { 
    var data = markers[index]; 
    bounds.extend(new google.maps.LatLng(data.lat, data.lng)); 
} 
map.fitBounds(bounds);