Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using getPosition() with Google Advanced markers

Tags:

javascript

Does anyone know why "getPosition()" doesn't work with the new Google map Advanced markers?

Below is a link to a test page where "getPosition()" works fine with a legacy marker, while the Advanced marker gets an "Uncaught TypeError: marker2.getPosition is not a function" error message.

Refer to the console.log for results.

Test page:

https://whowillbethenextonline.com/advanced-marker-get-position.html

Thanks,

Bennie

P.S. I tried all kinds of things, and looked everywhere, but haven't been able to figure it out, nor find an answer online.

P.P.S. Finally! After looking at the code for this Google maps tutorial "Make markers draggable" example,

[https://developers.google.com/maps/documentation/javascript/examples/advanced-markers-draggable],

I changed "marker2.getPosition()" to "marker2.position.lat" and "marker2.position.lng" and the remote Advance marker button worked!

See example page: [ https://whowillbethenextonline.com/advanced-marker-get-position-2.html ].

I tried "marker.position.lat" and "marker.position.lng" for the legacy marker and got something other than the lat and lng, as you will be able to see in your console.log() gizmo.

like image 433
Bennie Avatar asked Aug 28 '26 12:08

Bennie


2 Answers

Old Markers (Legacy) support getPosition() method to get their coordinates.

As Google introduced AdvancedMarkerElement now marker location coordinates can be retrieved using just .position

Example:

Old Marker elements marker.getPosition()

New Advanced Marker marker.position

like image 150
kerri Avatar answered Aug 30 '26 03:08

kerri


After updating to advanced markers I had a very similar issue, with the same error. In the end I had to add the lat and long into .position

Here is the original line which didn't work with advanced markers

map.panTo(clickedMarker.getPosition());

Here is my new line of code that worked

map.panTo(new google.maps.LatLng(clickedMarker.position.lat, clickedMarker.position.lng));
like image 20
Ian Avatar answered Aug 30 '26 02:08

Ian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!