我得到了关于如何在中心获得折线的问题的答案,但这是问题所在。 当我想看地图时。地图仅在我的div的左上角初始化。当我调用这个函数时:
setTimeout(function() {
google.maps.event.trigger(map,'resize');
}, 200);
我可以看到整个div中的地图,但是折线没有缩放,而是在左上角。
我该怎么办?
我的代码:
全球定义:
var flightPlanCoordinates=[];
var flightPath;
var map;
var myLatLng = new google.maps.LatLng(0, 0);
var mapOptions = {
zoom: 5,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
后来用过的代码......
$.ajax({
type: 'POST',
url: 'history.php',
data: {
'd_year1':$('#select-choice-year1').val(),
'd_month1': $('#select-choice-month1').val(),
'd_day1':$('#select-choice-day1').val(),
'd_year2':$('#select-choice-year2').val(),
'd_month2': $('#select-choice-month2').val(),
'd_day2':$('#select-choice-day2').val()
},
success: function(data)//callback to be executed when the response has been received
{
if(data=="[]")
{
alert("None");
}else
{
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);
zoomToObject(flightPath);
}
}
});
用于缩放和居中折线的功能:
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);
setTimeout(function() {
google.maps.event.trigger(map,'resize');
}, 200);
}
那么如何真正初始化地图以适应整个div以及如何缩放和居中折线以适应地图?
答案 0 :(得分:4)
更改缩放和放大中心功能:
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);
setTimeout(function() {
google.maps.event.trigger(map,'resize');
map.fitBounds(bounds);
}, 200);
}
或者在调用之前触发map resize事件(在AJAX回调中)。