0

I am auto updating google maps every x seconds in jquery/javascript.

When location is changed map changes the zoom level (expected).

But i want to achieve this:

If user has manually changed zoom level, then the map should keep/persist the user-set zoom level.

Code looks similar to this:

  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();

I will prefer standard google map events like zoom_changed instead of creating custom events for user zoom.

Any directions on this subject?

hitesh israni
  • 1,742
  • 4
  • 25
  • 48

1 Answers1

1

Try like this:

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);
}

where map is your google.maps.Map object.

P.S.: For preventing both center and zoom from being changed by setDirections method, you can specify preserveViewport=true of google.maps.DirectionsRendererOptions object.

Engineer
  • 47,849
  • 12
  • 88
  • 91
  • preserveViewport=true wont help. because i want center of the map to change – hitesh israni Jul 05 '12 at 10:02
  • @mashit Yeah, I doubted about it, that's why I put that sentence on *PS* section. – Engineer Jul 05 '12 at 10:04
  • yeah. is there a way to differentiate whether zoom was changed by user or it was changed because of map auto update? – hitesh israni Jul 05 '12 at 10:06
  • @mashit No. You need to handle the all such interactions made by user. – Engineer Jul 05 '12 at 10:08
  • for auto updating/refreshing the map, i am changing start location to current location every x seconds. is there a better approach for this? – hitesh israni Jul 05 '12 at 10:09
  • and how to handle such interactions. any directions you can provide? – hitesh israni Jul 05 '12 at 10:09
  • 1
    @mashit About interactions, it'll not be so easy. You need to listen all `mousedown` and `mousewheel` events on map's div. Then check mouse's x and y positions relative to zoom's controlbar. Also `zoom_change`,`dblclick` events on `google.maps.Map` object,to perform some checkings. I think, it's hard task. – Engineer Jul 05 '12 at 10:18
  • yes. i thought so. cant this be achieved using zoom_changed event of map per se? – hitesh israni Jul 05 '12 at 10:23
  • I don't think ,it will cover all the cases. Maybe some tricks by `setTimeout`,`setInterval` can do something, but it would be too far from completeness. – Engineer Jul 05 '12 at 10:27
  • 1
    @mashit You may ask in StackOverlow such a question. Maybe there could be other thoughts than mine. – Engineer Jul 05 '12 at 10:28