Unable to call getPosition method in google map api v3

I want to use the getPosition method 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 can not name getPosition.

 Uncaught TypeError: Cannot call method 'getPosition' of undefined 

Can someone explain this? Thanks

0
source share
1 answer

As your question matters, mArray [count] .getPosition () is executed before the click event is triggered (but after defining it), this code is not executed until the 'click' occurs. This should work (but not sure why you would like 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(); }); 
+1
source

Source: https://habr.com/ru/post/1215806/


All Articles