This is the Google Places provider from the PHP Geocoder. This is a READ ONLY repository. See the main repo for information and documentation.
composer require geocoder-php/google-maps-places-provider
https://developers.google.com/places/web-service
This provider often requires extra data when making queries, due to requirements of the underlying Places API.
This provider supports two different modes of geocoding by text.
This is the default mode. It required an exact places name. It's not very forgiving, and generally only returns a single result
$results = $provider->geocodeQuery(
GeocodeQuery::create('Museum of Contemporary Art Australia')
);
This mode will perform a search based on the input text.
It's a lot more forgiving that the find
mode, but results will contain all fields and thus be billed at the highest rate.
$results = $provider->geocodeQuery(
GeocodeQuery::create('art museum sydney')
->withData('mode', GoogleMapsPlaces::GEOCODE_MODE_SEARCH)
);
around location (which is similar to reverse geocoding, see below):
$results = $provider->geocodeQuery(
GeocodeQuery::create('bar')
->withData('mode', GoogleMapsPlaces::GEOCODE_MODE_SEARCH)
->withData('location', '-32.926642, 151.783026')
);
country matches a country name or a two letter ISO 3166-1 country code. If you only use the "region" parameter, you will not be guaranteed to have results on the region, as the documentation indicates Region:
The region parameter will only influence, not fully restrict, results from the geocoder.
$results = $provider->geocodeQuery(
GeocodeQuery::create('montpellier')
->withData('components', 'country:FR');
);
Three options available for reverse geocoding of latlon coordinates:
- mode
search
+ type (e.g.)bar
: uses Google Place API Text search, requirestype
- is similar to: Search around location (see previous section)
- mode
nearby
+ rankbydistance
: uses Google Place API Nearby search, requirestype/keyword/name
- mode
nearby
+ rankbyprominence
: uses Google Place API Nearby search, requiresradius
Default mode: search
(because of backward compatibility). When using mode nearby
default rankby: prominence
.
Mode search
+ type and mode nearby
+ type/keyword/name are very similar.
Mode search
gives formatted_address, mode nearby
gives vicinity instead. E.g.:
search
: has "formatted_address": "7 Cope St, Redfern NSW 2016"nearby
: has "vicinity" instead: "7 Cope St, Redfern"
Examples
$results = $provider->reverseQuery(
ReverseQuery::fromCoordinates(-33.892674, 151.200727)
// ->withData('mode', GoogleMapsPlaces::GEOCODE_MODE_SEARCH) // =default
->withData('type', 'bar') // requires type
);
$address = $results->first()->getFormattedAddress();
$results = $provider->reverseQuery(
ReverseQuery::fromCoordinates(-33.892674, 151.200727)
->withData('mode', GoogleMapsPlaces::GEOCODE_MODE_NEARBY)
//->withData('rankby','prominence'); // =default
->withData('radius', 500) // requires radius (meters)
);
$vicinity = $results->first()->getVicinity();
$results = $provider->reverseQuery(
ReverseQuery::fromCoordinates(-33.892674, 151.200727)
->withData('mode', GoogleMapsPlaces::GEOCODE_MODE_NEARBY)
->withData('rankby','distance');
->withData('keyword', 'bar') // requires type/keyword/name
);
Contributions are very welcome! Send a pull request to the main repository or report any issues you find on the issue tracker.