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 prevents searching for countries #1107

Open randriese opened 3 years ago

randriese commented 3 years ago

I'm using the Mapbox Provider in my API to fetch coordinates for given entities. These entities can be either:

However, the current implementation of the mapbox provider prevents from returning anything that doesn't contain a context.

When I run this snippet and watch the console, I see that the query to Mapbox is correct and data is returned. $locations = $mapboxGeocoder->geocodeQuery( GeocodeQuery::create($country->getName()) ->withLocale('nl') ->withData('location_type', 'country') );

However, the fetchUrl method in Mapbox.php simply breaks when a result without context is returned by Mapbox. https://github.com/geocoder-php/mapbox-provider/blob/master/Mapbox.php#L283

When I remove that piece of code, and add an additional check here to check if context is available before iterating through it, the code returns the requested items. https://github.com/geocoder-php/mapbox-provider/blob/master/Mapbox.php#L301

This isn't a clean solution and additional models are required to make it a better implementation. However, the repo is read-only so I cannot help with the required changes

jbelien commented 3 years ago

This isn't a clean solution and additional models are required to make it a better implementation. However, the repo is read-only so I cannot help with the required changes

You can submit a PR in our main repository (this one). Mapbox provider is located here: https://github.com/geocoder-php/Geocoder/tree/master/src/Provider/Mapbox

randriese commented 3 years ago

It seems that quite a big PR is required for the library to support Countries, Cities or Districts as valid query results... I'll see what I can do.