Event from moving a marker in Google Maps

My organizations gets calls when a client installs a weather station. This always includes a latitude and longitude that is then used to record where the station is. The installers generally gets a latitude and longitude from their phone; different people use different services that have different imagery so we like to homogenize it with Google’s. I’ve made a form that has you fill out the Lat/Long and then you press a button/enter to have the map place a marker.

What I would like to be able to do is then set up an event for when the marker’s location is changed that then updates the text in the text fields that will then be saved to a database. I know that is an event listed in the docs: position_changed().

I have no idea how to access it however, I’ve read the docs on events but still really not sure how to trigger it or get the existing map’s marker data.

Clone link:
https://anvil.works/build#clone:EE6PDCOHI5EKB6XY=IPG3TUJADB3MBCUNHYQAMM4T

You actually shared a link to run your app rather than to clone it. (You’re not alone.)

I think I have the idea without seeing your code, though. (Disclaimer: I haven’t actually tried this or used the GoogleMaps integration at all.)

Whenever your code adds a marker to the map, run this method on it (before you lose track of it):

marker.add_event_handler("position_changed", marker_position_changed)

Then you define what happens in response this way:

def marker_position_changed(sender, **event_args):
  marker = sender
  print(marker.position) # or store to your database, etc.
1 Like

Thanks, That got me headed in the right direction:

marker.add_event_handler("position_changed", self.marker_position_changed)

def marker_position_changed(self, **event_args):
  marker = event_args.get('sender')
  print(marker.position) # or store to your database, etc.  

This is now printing out the new latitude and longitude that the marker is moved to.

Appreciate the guidance!

2 Likes