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.)