Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get center coords after drag & pan finished

I would like to keep track of the coordinates of the center of my map. So far I've been using this:

// On Drag End
google.maps.event.addListener(map, 'dragend', function() { 
    $('.map_center_coords .latitude').html( map.getCenter().lat() );
    $('.map_center_coords .longitude').html( map.getCenter().lng() );
});

which works by getting the coords at the end of the drag event.

The problem is I'm now using map.panTo to move to a certain location.

Is there an event for basically "whenever the center has *finished* moving in any way"?

As mentioned by geocodezip, I forgot about the center_changed event. But that event is continuously fired while the map is being dragged/panned.

Ideally I'm looking for an event that is only fired once, after any dragging/panning is done.

like image 244
Juicy Avatar asked Aug 28 '14 02:08

Juicy


1 Answers

Observe the idle-event instead(This event is fired when the map becomes idle after panning or zooming):

    google.maps.event.addListener(map,'idle',function(){
      if(!this.get('dragging') && this.get('oldCenter') && this.get('oldCenter')!==this.getCenter()) {
        //do what you want to
      }
      if(!this.get('dragging')){
       this.set('oldCenter',this.getCenter())
      }

    });

    google.maps.event.addListener(map,'dragstart',function(){
      this.set('dragging',true);          
    });

    google.maps.event.addListener(map,'dragend',function(){
      this.set('dragging',false);
      google.maps.event.trigger(this,'idle',{});
    });
like image 170
Dr.Molle Avatar answered Oct 20 '22 17:10

Dr.Molle