javascript 谷歌地图:API 3,如何在中心设置折线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13027516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google maps: API 3, how to set polyline in the center
提问by user123_456
Hi all I need to be able to center the polyline in the center of the map
大家好,我需要能够将折线居中在地图的中心
Only the necessery part of the code...
只有代码的必要部分......
success: function(data) {//callback to be executed when the response has been received
data = JSON.parse(data);
for (var i=0;i<data.length;i++) {
flightPlanCoordinates[i] = new google.maps.LatLng(data[i].x,data[i].y);
}
map = new google.maps.Map(document.getElementById("map_find"),
mapOptions);
flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
strokeColor: "#8a2be2",
strokeOpacity: 1.0,
strokeWeight: 3
});
flightPath.setMap(map);
map.setZoom(5);
map.setCenter(flightPlanCoordinates);
}
This is not doing what I need..My map is not centered and not zoomed, how to achieve this?
这不是我需要的。我的地图没有居中也没有缩放,如何实现?
回答by Marcelo
You can use this function, calling it with the polyline as parameter:
您可以使用此函数,以折线为参数调用它:
function zoomToObject(obj){
var bounds = new google.maps.LatLngBounds();
var points = obj.getPath().getArray();
for (var n = 0; n < points.length ; n++){
bounds.extend(points[n]);
}
map.fitBounds(bounds);
}
Call it:
称它为:
flightPath.setMap(map);
zoomToObject(flightPath);
Just make sure that your map object is a global variable.
只要确保您的地图对象是一个全局变量。