Luyz Flores Rangel
Luyz Flores Rangel

Reputation: 11

How can I change marker position eventually in google maps api?

I have a problem with google maps api, when I change the marker position constantly, this leave a trace on map and I need move the marker without leaving a trace. I actually get the latitude and the longitude from a database in firebase and need represent the route of a car.

var mapOptions = {

 center: new google.maps.LatLng(20.615977, -103.339358), 
 zoom: 12

};

var map = new google.maps.Map(document.getElementById("map"), mapOptions);    

firebase.database().ref('route').on('value', function(data) {

var start = new google.maps.Marker({
    position: {lat: latitud, lng: longitud}, 
    map: map,
    icon: '../icon/car.png'
});

var myLatlng = new google.maps.LatLng(data.val().latitude, data.val().longitude);

start.setPosition(myLatlng);
start.setMap(map);   

});

Upvotes: 0

Views: 1384

Answers (1)

PersonThing
PersonThing

Reputation: 388

You're creating a new marker in every event. Instead, create the marker before the event handler (and call setMap there), and in your handler, just do setPosition() to the data's new lat/lng.

Upvotes: 1

Related Questions