0

我在 jquery/javascript 中每隔 x 秒自动更新一次谷歌地图。

更改位置时,地图会更改缩放级别(预期)。

但我想实现这一点:

如果用户手动更改了缩放级别,则地图应保持/保持用户设置的缩放级别。

代码与此类似:

  function calcRoute(user) {
     if(user)
     {
        start = document.getElementById("start_location").value;
     }
     end = document.getElementById("end_location").value;
     var request = {
        origin:start, 
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.WALKING
    };

    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        $('#txt_dur').text(response.routes[0].legs[0].duration.text);
        $('#txt_dist').text(response.routes[0].legs[0].distance.text);      

      }
    });
    //if(userZoom==true)
    //    map.setZoom(current_zoom);

  }

      function autoUpdate() {
         navigator.geolocation.getCurrentPosition(function(position) {
         var newPoint = new google.maps.LatLng(position.coords.latitude,
                                          position.coords.longitude);

         start = newPoint.toString();
         calcRoute(false);
      });

    setTimeout(autoUpdate, 5000);
   }

    autoUpdate();

我会更喜欢标准的谷歌地图事件,比如 zoom_changed,而不是为用户缩放创建自定义事件。

关于这个主题的任何方向?

4

1 回答 1

1

试试这样:

if (status == google.maps.DirectionsStatus.OK) {
    var tmpZoom = map.getZoom();   //added line
    directionsDisplay.setDirections(response);
    map.setZoom( tmpZoom );        //added line

    $('#txt_dur').text(response.routes[0].legs[0].duration.text);
    $('#txt_dist').text(response.routes[0].legs[0].distance.text);
}

map你的google.maps.Map对象在哪里。

PS:为了防止两者都centerzoom方法更改,setDirections您可以指定google.maps.DirectionsRendererOptions对象。preserveViewport=true

于 2012-07-04T13:10:08.643 回答