class LocationIQConverter: def __init__(self): # Create LocationIQ API object to convert places into their coordinates self.geocoder = LocationIQ(ACCESS_TOKEN) #Geolocate the received place returning its latitude and longitude among other properties def place_to_coordinates(self, place=None): if (place == None): return None results = self.geocoder.geocode(place) return results
class LocationIqModel: def __init__(self): """ Create LocationIQ API object to convert places into their coordinates """ self.geocoder = LocationIQ(config.LOCATIONIQ_TOKEN) def get_coordinates(self, place: str, max_tries: int = 10, time_between_tries: int = 1) -> dict: """ Geolocate the received place returning its latitude and longitude among other properties. Arguments: place (:obj:`str`): Plate to convert to coordinates. max_tries (:int, optional): Number of tries to obtain location info. time_between_tries (:int, optional): Time between tries to obtain location info. Returns: :obj:`dict`: contanining the information in matter of geocoding of the given place. """ for i in range(max_tries + 1): try: geocoding_data = self.geocoder.geocode(place) if geocoding_data: geocoding_data = geocoding_data[0] result = { 'lat': float(geocoding_data['lat']), 'lon': float(geocoding_data['lon']), 'address': geocoding_data['address'] } else: result = {'lat': None, 'lon': None, 'address': None} return result except LocationIqRequestLimitExeceeded: time.sleep(time_between_tries) except: pass return None
from locationiq.geocoder import LocationIQ, LocationIqNoPlacesFound # Constants KEY = 'ENTER LOCATIONIQ TOKEN HERE' SIZE = '800x800' place = input("Enter the place ") if place: # Map zoom zoom = int(input("Enter zoom level: ")) # locationIQ stuff geocoder = LocationIQ(KEY) try: jsondata = geocoder.geocode(place) except LocationIqNoPlacesFound: print("Couldn't find the entered location!") exit(1) # use the lats and longs in json data dict latitude = jsondata[0]['lat'] longitude = jsondata[0]['lon'] static_map = f"https://maps.locationiq.com/v2/staticmap?key={KEY}&markers={latitude},{longitude}|icon:large-red-cutout;&zoom={zoom}&size={SIZE}" # fetch map from static locationiq api map = requests.get(static_map) # Convert map to byte data buf = BytesIO()