Showing approximate location on a map based on address

Some apps such as AirBnB show an approximate location for their clients - they want to show you what part of town something is in, but they don’t want to give away their client’s exact location.

This example does just that. You can enter an address and a circle will appear on the map, with a 100 metre radius and centred up to 100 metres north/south and east/west of the actual location.

https://anvil.works/build#clone:B5AEHTM4D2MQHIZ2=M7QPEFFFUWH6ANVZFETKXA6V

It uses the ArcGIS geocoder on the server side to convert the address into a latitude and longitude:

from geopy.geocoders import ArcGIS

@anvil.server.callable
def geocode(address):
  geolocator = ArcGIS()
  location = geolocator.geocode(address)
  return location.latitude, location.longitude

On the client side, it adds a GoogleMap.Circle marker to the map component:

    if hasattr(self, 'marker'):
      self.marker.remove_from_parent()

    self.marker = GoogleMap.Circle(
      center=GoogleMap.LatLng(lat+offset_lat, lng+offset_lng),
      radius=100
    )

    self.map_1.add_component(self.marker)

The offsets are generated using the rule of thumb that 1 degree of latitude is approximately 111,111 metres, and 1 degree of longitude is approximately 111,111*cos(latitude) metres (thanks, GIS Stackexchange)

    offset_lat = 200./111111. * random.uniform(-0.5, 0.5)   # Random offset of approx 200 meters
    offset_lng = 200./(111111.*cos(lat)) * random.uniform(-0.5, 0.5)   # Random offset of approx 200 meters

Feel free to clone it, use it and modify it.

https://anvil.works/build#clone:B5AEHTM4D2MQHIZ2=M7QPEFFFUWH6ANVZFETKXA6V

(I’ve added an Apache open-source license in a Module for the avoidance of doubt.)

8 Likes

this doesn’t have request rate limits like the google map geocode api has right?

1 Like

Actually, there is a rate limit when you use ArcGIS without a token, so you’re better off using our GoogleMap.geocode interface (which is client-side, so better anyway) and pasting a Google Maps API key into the Google Service:

If you do want to use ArcGIS in production, you’ll need to get an account and authenticate:

  geolocator = ArcGIS(username='bob.hoskins@gmail.com', password='brazil')

ArcGIS’s free plan allows 1,000,000 geocode requests a month so that should get you a long way :wink:

1 Like