GoogleMap geocode from server module

I’m pretty sure I know the answer but I’ll ask anyway. I have some Lat/Lng that I’d like to resolve to street addresses on the server side. Is this possible? Is it a reasonable feature request?

The answer is yes, that’s possible!

We have geopy installed in Server Modules (for full package list, see here - we can install any more that you ask for.)

Here’s a simple server function that does a reverse geocode lookup using a free API:

from geopy.geocoders import ArcGIS

@anvil.server.callable
def geocode(lat, lng):
  geolocator = ArcGIS()
  location = geolocator.reverse((lat, lng))
  print(location.address)

You can also do geocoding from the client side using our GoogleMap integration:

addresses = GoogleMap.geocode(location=lat_lng)
print(addresses[0].formatted_address)

Here’s the relevant section of the reference docs.

Here’s an app that prints the address when you click on a map:

https://anvil.works/build#clone:EORJ5TAP6BQ74KCY=FL2QBYLYIZB4TXDUB6DSINTM

(Note: The results differ slightly depending on which API you use, and most APIs give you a list of nearby addresses to choose from.)

2 Likes

is this broken? I am using the basic account and have followed the code here and I am met with the below when clicking on the map:

ExternalError: Geocode failed: REQUEST_DENIED

Is there a limitation here that the docs aren’t covering?

(EDIT: To specify I am using the GoogleMaps integration on client side)

See the other post on this topic: GoogleMap client side integration broken? (Geocoding)