Daniel Listyo Emanuel
Daniel Listyo Emanuel

Reputation: 337

Cannot call method getPosition in google map api v3

I want to use method getPosition in this code :

alert("Tap location in this map");
google.maps.event.addListener(map, 'click', function(event) {
    mArray[count] = new google.maps.Marker({
        position: event.latLng,
        map: map
    });
});
mArray[count].getPosition();

But I cannot call getPosition.

Uncaught TypeError: Cannot call method 'getPosition' of undefined

count is a global variable.

var count=0;
var mArray = [];

Can someone explain it ?

Upvotes: 0

Views: 4461

Answers (1)

geocodezip
geocodezip

Reputation: 161334

The way your question has it, mArray[count].getPosition() executes before the click event runs (but after it has been defined), that code doesn't execute until the 'click' happens. This should work (but not sure why you would want to do it this way):

alert("Tap location in this map");
google.maps.event.addListener(map, 'click', function(event) {
    mArray[count] = new google.maps.Marker({
        position: event.latLng,
        map: map
    });
    mArray[count].getPosition();
});

Upvotes: 1

Related Questions