Saurabh
Saurabh

Reputation: 1406

How to change the position of marker from a javascript function?

I have to change the position of a marker on Google map from a javascript function. How can I achieve that?

Upvotes: 46

Views: 109320

Answers (3)

Recep Karabıçak
Recep Karabıçak

Reputation: 2083

You can use setPosition function of the marker class

function changeMarkerPosition(marker) {
    var latlng = new google.maps.LatLng(-24.397, 140.644);
    marker.setPosition(latlng);
}

Upvotes: 129

Argiropoulos Stavros
Argiropoulos Stavros

Reputation: 9533

First off you must store the marker in an array when you create it so you can have access to it afterwards.
Then change the position with marker.setPosition() as solidrevolution mentioned.

Upvotes: 2

satish kumar tak
satish kumar tak

Reputation: 1

Try this:

var x = document.getElementById("demo");
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}
function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude +
        "<br>Longitude: " + position.coords.longitude;
}

Upvotes: -9

Related Questions