Exemple #1
0
def locate(area: str = None) -> str:
    """Returns current location.

    area: Looks for specific details of an address while locating.
          Default: None

    Find and returns current global position using reverse lookup via Google Maps API.

    Note: Function uses Google Maps for retreiving latitude and longitude
    using Google Maps API. Hence it is necessary to generate the API key first
    before running this function.
    You can generate it here: https://console.developers.google.com

    Caution: If you run the function without passing valid API key, it will
    raise an exception.
    """
    import os
    from geocoder import osm
    from googlemaps import Client

    try:
        if check_internet():
            # Passing Google maps API key.
            gmaps = Client(key=os.environ.get('CHARLOTTE_MAPS_KEY'))
            # Finding current latitude and longitude details.
            current_coords = gmaps.geolocate()
            location = osm(list(current_coords['location'].values()),
                           method='reverse')
            if area is not None:
                try:
                    # Returns particular address detail only.
                    return location.json[area]
                except:
                    return _NO_RESPONSE
            else:
                try:
                    # Returns complete address.
                    return location.json['address']
                except:
                    return _NO_RESPONSE
        else:
            # Returns None if no internet connection is available.
            return None
    except Exception as error:
        print('An error occured while performing this operation because of'
              f' {error} in function "{stack()[0][3]}" on line'
              f' {exc_info()[-1].tb_lineno}.')
Exemple #2
0
def _get_coords(location: Optional[Text] = None) -> Tuple:
    """Returns coords for the asked location.

    location: Location or the address to be converted to latitude and
              longitude.
              Default: None

    Find and returns current global position and the city using reverse
    lookup via Google Maps API.

    Note: Function uses Google Maps for retreiving latitude & longitude
    using it`s API. Hence it is necessary to generate the API key first
    before running this function.
    You can generate it here: https://console.developers.google.com

    Caution: If you run the function without passing valid API key, it
    will raise an exception.
    """
    import os
    from geocoder import osm
    from googlemaps import Client

    try:
        # Passing Google maps API key.
        map = Client(key=os.environ.get('CHARLOTTE_MAPS_KEY'))
        # Finding current latitude and longitude coordinates.
        if location:
            curr = map.geocode(location)
            coords = (curr[0]['geometry']['location']['lat'],
                      curr[0]['geometry']['location']['lng'])
        else:
            curr = map.geolocate()
            coords = (curr['location']['lat'], curr['location']['lng'])
        # Reverse mapping the coordinates to find out the city name.
        area = osm(coords, method='reverse')
        loc_list = ['city', 'town', 'suburb', 'state', 'region', 'country']
        for idx in loc_list:
            if area.json.get(idx, None) is not None:
                loc = area.json[idx]
                break
        return coords, loc
    except Exception as error:
        print('An error occured while performing this operation because of '
              f'{error} in function "{stack()[0][3]}" on line '
              f'{exc_info()[-1].tb_lineno}.')
        return (None, None), None