Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse animation for Google Map marker removal?

I know I can animate the "adding" of a marker on a google map, a la https://developers.google.com/maps/documentation/javascript/overlays#MarkerAnimations

Is there anyway I can do the reverse animation for removing the marker from the map? I'd like it to fly back up to the top of the map on marker removal... is that possible?

Here's my remove code so far (just removes it from the map, no animation):

// TODO figure out if there is a way to animate this removal, like the add
$.contextualMap.prototype.removeMarker = function(m) {
  m.mapMarker.setMap(null);
  m.mapMarker = null;
};
like image 654
neezer Avatar asked Apr 16 '12 17:04

neezer


People also ask

How do I get rid of markers on Google Maps?

Click the marker to reveal the editing options. Click the "Delete" link in the lower-left corner of the dialog box.

How do I animate a marker on Google Maps?

If you want to animate marker back-and-forth, just toggle setPosition each second. setTimeout(function() { var newPosition = /* select new position */ marker. setPosition(newPosition) }, 1000);

How do you remove a specific marker?

Inside the Info Window of the marker, there's an HTML button (Delete) which when clicked triggers the DeleteMarker JavaScript function. This function accepts the ID of the marker as a parameter and using this ID the selected marker is removed from the Map. };

How do I get rid of blue markers on Google Maps?

If you want to remove the pin from Google Maps, simply right click on it and select "Remove this destination." Poof, it's gone.


1 Answers

As google.maps.Animation does not support reverse animation of droping, you need to write your own script for animating the marker.

You could write something like this:

function removeMarkerWithAnimation(map, marker){
    (function animationStep(){
        //Converting GPS to World Coordinates
        var newPosition = map.getProjection().fromLatLngToPoint(marker.getPosition());

        //Moving 10px to up
        newPosition.y -= 10 / (1 << map.getZoom()); 

        //Converting World Coordinates to GPS 
        newPosition = map.getProjection().fromPointToLatLng(newPosition);
        //updating maker's position
        marker.setPosition( newPosition );
        //Checking whether marker is out of bounds
        if( map.getBounds().getNorthEast().lat() < newPosition.lat() ){
            marker.setMap(null);
        }else{
            //Repeating animation step
            setTimeout(animationStep,10);
        }
    })();
}

Here is DEMO:

like image 91
Engineer Avatar answered Sep 24 '22 19:09

Engineer