简体   繁体   English

为Google Maps v3设置缩放功能不适用于准备好的文档

[英]set zoom for google maps v3 does not work on document ready

This doesn't work on document ready, but it works on ajax updates. 这不适用于准备好的文档,但适用于Ajax更新。

map.fitBounds(bounds);
map.setZoom(map.getZoom() - 1);

and I don't know why when I am using the same function of updateBounds(). 而且我不知道为什么当我使用相同的updateBounds()函数时。 I am trying to accommodate the infowindows after I call fitbounds for the markers by zooming out one level. 在我通过缩小一级为标记调用fitbounds之后,尝试容纳信息窗口。 Any other solutions for accommodating info windows to prevent clipping would be appreciated, or any suggestions to make my current solution work would be great. 用于容纳信息窗口以防止剪切的任何其他解决方案将不胜感激,或者使我当前的解决方案有效的任何建议都将是不错的。

var map, mapCenter = new google.maps.LatLng(0, 0);
var bounds = new google.maps.LatLngBounds();
var driverMarker;
var orderMarker;
var LatLngO;
var LatLngD;

function initializeMap() {
    map = new google.maps.Map(document.getElementById('map_canvas'), {
        zoom: 13,
        center: mapCenter,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        zoomControl: false
    });
}
function getOrder() {
    var lat = $('#orderLat').text();
    var lng = $('#orderLng').text();
    LatLngO = new google.maps.LatLng(lat, lng);
    orderMarker = new google.maps.Marker({ map: map, position: LatLngO, icon: "images/greenHouse.png" });
}
function getDriver() {
    var lat = $('#driverLat').text();
    var lng = $('#driverLng').text();
    LatLngD = new google.maps.LatLng(lat, lng);
    driverMarker = new google.maps.Marker({ map: map, position: LatLngD, icon: "images/greenCircle.png" });
}
function update() {
    $.get("driverLocation.aspx?id=" + $('#driverId').text() + "&orderId=" + $('#orderId').text(), function (data) {
        $("#LatLng").html(data);
        updateDriver();
        updateBounds();
    });    
}
function updateDriver() {
    var lat = $('#driverLat').text();
    var lng = $('#driverLng').text();
    var LatLng = new google.maps.LatLng(lat, lng);
    driverMarker.setPosition(LatLng);
}
function updateBounds(){
    bounds.extend(orderMarker.getPosition());
    bounds.extend(driverMarker.getPosition());
    map.fitBounds(bounds);
    map.setZoom(map.getZoom() - 1);
}

$(document).ready(function () {
    initializeMap();
    getOrder();
    getDriver();
    updateBounds();
    updateTimer = setInterval('update()', 10000);
});

map.getZoom will not get the new bounds unless you call it inside an event handler that is listening for the zoom_changed event ( .fitBounds sets the zoom asynchronously and fires the zoom_changed event after setting the new zoom) map.getZoom不会获得新的边界,除非您在侦听zoom_changed事件的事件处理程序中调用它( .fitBounds异步设置缩放并在设置新的zoom后触发zoom_changed事件)

map.fitBounds(bounds);
google.maps.event.addEventListenerOnce(map,'zoom_changed', function() {
  map.setZoom(map.getZoom() - 1);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM