[Python] Need help dealing with certain address scenarios in large cities and Google Places API

Discussão em 'Python' iniciado por Stack, Novembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I have an issue with the London area using Google Places API and the endpoint for autocomplete. Why are we using autocomplete? Because our input is at time ambiguous. The background story is that we ask the customer for a specific location.

    Anyway, imagine the following address "13 Manor Close" being searched using the Google Places Autocomplete API with the following code snippet.

    import requests
    import json

    """Autocomplete search parameters
    """
    api = "XXXX"
    address = "13 Manor Close"
    lat = "51.5072"
    long = "-0.1138486"
    radius = 50000
    language = "en"

    """Google Autocomplete payload
    """
    payload = {
    "input": address,
    "locationRestriction": {
    "circle": {
    "center": {
    "latitude": lat,
    "longitude": long
    },
    "radius": radius
    }
    }
    }

    """Get suggestions using autocomplete api
    """
    response = requests.request(
    "POST",
    "https://places.googleapis.com/v1/places:autocomplete",
    headers={"Content-Type": "application/json; charset=utf-8",
    "X-Goog-Api-Key": api},
    json=payload
    )

    response_dict = json.loads(response.text)

    """Iterate over returned suggestions
    """
    for res in response_dict["suggestions"]:

    place_id = res["placePrediction"]["placeId"]

    """Use Google Places Detail lookup for the suggested place id
    """
    place = requests.request(
    "GET",
    f"https://places.googleapis.com/v1/places/{place_id}?key={api}",
    headers={
    "Content-Type": "application/json; charset=utf-8",
    "X-Goog-Api-Key": api,
    "X-Goog-FieldMask": "*"
    }
    )

    place_dict = json.loads(place.text)
    print(place_dict)


    If we query the autocomplete with locationRestriction and radius of 50,000m we get 5 results back.

    {
    "description":"13 Manor Close, Bengeo, Hertford"
    },
    {
    "description":"13 Manor Close, Hatching Green, Harpenden"
    },
    {
    "description":"13 Manor Close, London"
    },
    {
    "description":"13 Manor Close, Aveley, South Ockendon"
    },
    {
    "description":"13 Manor Close, Horley"
    }


    Now here's the deal, the "13 Manor Close, London" returned in the above result in the postal code NW9, however the "13 Manor Close, London" also exists in SE28 and E17.

    13 Manor Close, London SE28 https://www.google.com/maps/place/1...try=ttu&g_ep=EgoyMDI0MTEyNC4xIKXMDSoASAFQAw==

    13 Manor Close, London NW9 https://www.google.com/maps/place/1...try=ttu&g_ep=EgoyMDI0MTEyNC4xIKXMDSoASAFQAw==

    13 Manor Close, London E17 https://www.google.com/maps/place/1...try=ttu&g_ep=EgoyMDI0MTEyNC4xIKXMDSoASAFQAw==

    And unfortunately Google only return at most 5 results for the autocomplete. If we search specifically for "13 Manor Close, London" only NW9 and E27 is returned, but SE28 is not.

    The only way to have them returned appears to be to pass the postal code in the search query for an example: 13 Manor Close SE28, but this is not something we can expect from the customer.

    Have I managed to find a corner case, or is the London mapping this broken? Open for discussion, questions and suggestions how to deal with this scenario.

    Continue reading...

Compartilhe esta Página