geocoder-php / Geocoder

The most featured Geocoder library written in PHP.
https://geocoder-php.org
MIT License
3.94k stars 517 forks source link

Mapbox provider not working with US zip codes #1086

Open timwood opened 3 years ago

timwood commented 3 years ago

When using the https://github.com/geocoder-php/mapbox-provider with Drupal's geocoder module, Mapbox is unable to geocode US zip codes. I've tried both +5 and +9 zip codes. Mapbox also seems to have issues with searching for example Rockville, MD (get results in New England 🤷‍♂️ ) yet searching for Rockville, Maryland gets the expected results. When using more complete addresses, geocoding works.

When using the ArcGis Online provider, the above examples work flawlessly.

The Mapbox geocoder format guide suggests that US zip code (either five or nine digit) should work: https://docs.mapbox.com/help/troubleshooting/address-geocoding-format-guide/#zip-code-formatting

Has anyone else experienced similar problems with the Mapbox provider?

Related issue on D.O. https://www.drupal.org/project/geocoder/issues/3176070

jbelien commented 3 years ago

Hello @timwood ,

We don't do any process of the result. We just provide a structured way to query an API and a structured way to return the result of an API (whatever API is used in both cases).

Do you mean that a query that's working on Mapbox API doesn't work using our Mapbox provider ? If so, could you provide an address and the result of the Mapbox API ? If the issue is on Mapbox side, unfortunately we can't do anything about it, you should contact Mapbox.

timwood commented 3 years ago

Hey @jbelien, The zip code I've been testing with is 20853 which is in Rockville, Maryland in the United States. An example address in this zip code is 4615 Sunflower Drive, Rockville, Maryland 20853, United States. I'm not 100% sure where to test the Mapbox geocoding API directly, but when I used this testing tool, https://docs.mapbox.com/help/demos/how-mapbox-works/how-geocoding-works.html, the API seems to return the intended location (autocomplete is in play, which also seems to use the API) when I type in the zip code. I'm not familiar enough with the Mapbox places API to query it directly.

timwood commented 3 years ago

Hi again @jbelien When debugging the geocoder-php/mapbox-provider to print out the URL it's sending to the Mapbox API, I see this:

https://api.mapbox.com/geocoding/v5/mapbox.places/20853.json?types=address&country=US&language=en&limit=5&access_token=redacted_token

When manually querying the API (using the URL above), this results in the following response, which has no results:

{
    "type": "FeatureCollection",
    "query": [
        "20853"
    ],
    "features": [],
    "attribution": "NOTICE: © 2021 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare."
}

If the types= value is changed from address to postcode, then we get the expected results: https://api.mapbox.com/geocoding/v5/mapbox.places/20853.json?types=postcode&country=US&language=en&limit=5&access_token=redacted_token

{
    "type": "FeatureCollection",
    "query": [
        "20853"
    ],
    "features": [
        {
            "id": "postcode.17761009594681230",
            "type": "Feature",
            "place_type": [
                "postcode"
            ],
            "relevance": 1,
            "properties": {},
            "text_en": "20853",
            "place_name_en": "Rockville, Maryland 20853, United States",
            "text": "20853",
            "place_name": "Rockville, Maryland 20853, United States",
            "bbox": [
                -77.1307239780142,
                39.0598630310048,
                -77.072221997467,
                39.1548479699547
            ],
            "center": [
                -77.08,
                39.09
            ],
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -77.08,
                    39.09
                ]
            },
            "context": [
                {
                    "id": "locality.5943981528969000",
                    "wikidata": "Q753733",
                    "text_en": "Aspen Hill",
                    "language_en": "en",
                    "text": "Aspen Hill",
                    "language": "en"
                },
                {
                    "id": "place.2385764576613990",
                    "wikidata": "Q327022",
                    "text_en": "Rockville",
                    "language_en": "en",
                    "text": "Rockville",
                    "language": "en"
                },
                {
                    "id": "district.14127387844028120",
                    "wikidata": "Q488659",
                    "text_en": "Montgomery County",
                    "language_en": "en",
                    "text": "Montgomery County",
                    "language": "en"
                },
                {
                    "id": "region.10710475061772660",
                    "wikidata": "Q1391",
                    "short_code": "US-MD",
                    "text_en": "Maryland",
                    "language_en": "en",
                    "text": "Maryland",
                    "language": "en"
                },
                {
                    "id": "country.19678805456372290",
                    "wikidata": "Q30",
                    "short_code": "us",
                    "text_en": "United States",
                    "language_en": "en",
                    "text": "United States",
                    "language": "en"
                }
            ]
        }
    ],
    "attribution": "NOTICE: © 2021 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare."
}

Is the mapbox-provider able to account for different types queries against the mapbox API? If so, how does it determine which to use?

jbelien commented 3 years ago

Is the mapbox-provider able to account for different types queries against the mapbox API? If so, how does it determine which to use?

Yes, it is indeed possible !

$query->withData('location_type', Mapbox::TYPE_POSTCODE)

See