geocoder-php/google-maps-places-provider

Geocoder Google Maps Places adapter

1.4.1 2023-07-09 14:05 UTC

This package is auto-updated.

Last update: 2024-11-14 11:31:26 UTC


README

Build Status Latest Stable Version Total Downloads Monthly Downloads Code Coverage Quality Score Software License

This is the Google Places provider from the PHP Geocoder. This is a READ ONLY repository. See the main repo for information and documentation.

Install

composer require geocoder-php/google-maps-places-provider

API Documentation

https://developers.google.com/places/web-service

Usage

This provider often requires extra data when making queries, due to requirements of the underlying Places API.

Geocoding

This provider supports two different modes of geocoding by text.

Find Mode

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')
);

Search Mode

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');
);

Reverse Geocoding

Three options available for reverse geocoding of latlon coordinates:

  • mode search + type (e.g.) bar: uses Google Place API Text search, requires type
    • is similar to: Search around location (see previous section)
  • mode nearby + rankby distance: uses Google Place API Nearby search, requires type/keyword/name
  • mode nearby + rankby prominence: uses Google Place API Nearby search, requires radius

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
    );

Contribute

Contributions are very welcome! Send a pull request to the main repository or report any issues you find on the issue tracker.