user2481034
user2481034

Reputation: 87

Google maps - how to add marker without layout refresh

Have problem with marker adding.

        var marker = new google.maps.Marker({
        position: myLatlng,
        title:"Hello World!"
    });

    // To add the marker to the map, call setMap();
    marker.setMap(map);

https://jsfiddle.net/73ogq84a/

Simple example, each seconds I put marker, page not reloading, but reloading some layout and we see effect of map reload.

It is possible to do that put marker smoothly.

But on this page http://www.flightradar24.com/simple_index.php all working fine, planes are flying and no effect of map reload.

Upvotes: 0

Views: 1736

Answers (2)

geocodezip
geocodezip

Reputation: 161384

Make the marker and map global. Don't recreate the map every time, just move the marker.

var map, marker, myLatlng;

setInterval(function () {
    if (!marker || !marker.setPosition) {
        marker = new google.maps.Marker({
            position: myLatlng,
            title: "Hello World!"
        });

        // To add the marker to the map, call setMap();
        marker.setMap(map);
    } else {
        marker.setPosition(myLatlng);
    }

}, 5000);

updated fiddle

Upvotes: 1

Yevgen Safronov
Yevgen Safronov

Reputation: 4033

Provide map property when you create marker:

function placeMarker(location) {
   var marker = new google.maps.Marker({
    position: location, 
    map: map,
    title:"Hello World!"
});

Upvotes: 1

Related Questions