biscolab / google-maps-php-sdk
Google Maps PHP SDK.
Installs: 156 955
Dependents: 0
Suggesters: 0
Security: 0
Stars: 13
Watchers: 4
Forks: 13
Open Issues: 3
Requires
- php: ^8.0
- guzzlehttp/guzzle: 5.*|6.*|7.*
Requires (Dev)
- ext-curl: *
- phpdocumentor/reflection-docblock: ~2.0
- phpunit/phpunit: 7.*|8.*|^9.0
- psr/log: ^1.0
- sami/sami: ^4.1
This package is auto-updated.
Last update: 2024-11-17 22:51:17 UTC
README
Google Maps PHP (unofficial library). This provide simple functions to work with Google Maps APIs. You can find further informations in Google Maps Platform Documentation
Google Maps provide many services, actually at this moment this package implements only Geocoding service but others will be available soon.
Reference & Documentation
Go to complete reference or read documentation
Google Maps Services
Ready
- Geocoding ☑️
- Elevation ☑️
- Places ☑️
- Time Zone ☑️
ASAP
- Directions (soon)
- Distance Matrix (soon)
- Road (soon)
Not scheduled
- Geolocation (not scheduled)
Installation
You can install the package via composer:
composer require biscolab/google-maps-php-sdk
Examples
Watch the examples
License
Documentation
This provide simple functions to work with Google Maps APIs. You can find further informations in Google Maps Platform Documentation
Reference
You can find complete API references
Google Maps Services
Ready
ASAP
- Directions (soon)
- Distance Matrix (soon)
- Road (soon)
Not scheduled
- Geolocation (not scheduled)
System requirements
PHP 7.1 or greater
Composer
Install the package via composer:
composer require biscolab/google-maps-php-sdk
Notice! The package is not yet stable, you may find trouble with your minimum stability settings. Further documentation coming asap.
Google Maps is a service supplied by Google and first of all you must register your app project on the Google Cloud Platform Console and get a Google API key which you can add to your app or website (source: official Google Maps documentation).
- Read the Pricing table
- Create your project by clicking on "Get started"
- Create project credentials
- Enable services (Geocoding API, Elevation API, etc...)
Google Console
Geocoding API
The Geocoding API is a service that provides geocoding and reverse geocoding of addresses.
Official Google Geocoding documentation
Initialize Geocoding Object
First of all replace YOUR_API_KEY
with your actual API key.
use Biscolab\GoogleMaps\Api\Geocoding; use Biscolab\GoogleMaps\Enum\GoogleMapsApiConfigFields; $geocoding = new Geocoding([ GoogleMapsApiConfigFields::KEY => 'YOUR_API_KEY' ]);
Get results
You have 3 different ways to retrieve data of your place!
Go to complete SDK reference
Geocoding (Latitude/Longitude Lookup) by address as string
getByAddress
accept following arguments:
$results = $geocoding->getByAddress('Insert your address here, city, postal code etc...');
Change response language using setLanguage
method
You can find the list of supported languages here: https://developers.google.com/maps/faq#languagesupport
// Set Spanish language $results = $geocoding->setLanguage('es')->getByAddress('Insert your address here, city, postal code etc...');
Reverse Geocoding (Address Lookup) by Location/LatLng object
getByLatLng
(getReverse
alias is deprecated) accept a LatLng
object which represents the location of the place.
$results = $geocoding->getByLatLng(new LatLng([ LatLngFields::LAT => $lat, LatLngFields::LNG => $lng, ])); // Alias `getReverse` deprecated!!! $results = $geocoding->getReverse(new LatLng([ LatLngFields::LAT => $lat, LatLngFields::LNG => $lng, ]));
Change response language using setLanguage
method
You can find the list of supported languages here: https://developers.google.com/maps/faq#languagesupport
// Set Spanish language $results = $geocoding->setLanguage('es')->getByLatLng(new LatLng([ LatLngFields::LAT => $lat, LatLngFields::LNG => $lng, ])); // Use the same way for getReverse (alias) method
By Place ID as string
getByPlaceId
accept as parameter the address the place ID.
$results = $geocoding->getByPlaceId('YOUR_PLACE_ID');
Use results
Results is/are a Biscolab\GoogleMaps\Http\GeocodingResultsCollection
object.
First thing you should know how many results there are in your GeocodingResultsCollection
using count
method.
$number_of_results = $results->count();
To retrieve the first result you can use the first
method:
$first_result = $results->first();
$first_result
is an instance of GeocodingResult
class and has the following methods:
Elevation API
The Elevation API provides elevation data for all locations on the surface of the earth, including depth locations on the ocean floor (which return negative values).
There are two types of request:
- Positional Requests
- Sampled Path Requests
At the moment this package support only Positional Requests but I'm working on Sampled Path Requests and it will be available soon.
Official Google Elevation documentation
Initialize Elevation Object
First of all replace YOUR_API_KEY
with your actual API key.
use Biscolab\GoogleMaps\Api\Elevation; use Biscolab\GoogleMaps\Enum\GoogleMapsApiConfigFields; $elevation = new Elevation([ GoogleMapsApiConfigFields::KEY => 'YOUR_API_KEY' ]);
Get results (Positional Requests)
First of all you have to prepare the locations
variable, it can be a single Location
object, an array of Location
objects or a polyline string.
Single Location object
Create a Location object using latitude and longitude.
// get results by single Location object $locations = new Location([ LatLngFields::LAT => 39.73915360, LatLngFields::LNG => -104.9847034, ]);
Array of Location objects
Using multiple Location objects inside an array
// or by multiple Location objects $locations = [ new Location([ LatLngFields::LAT => 39.73915360, LatLngFields::LNG => -104.9847034, ]), // ... more locations new Location([ LatLngFields::LAT => 50.123, LatLngFields::LNG => 99.456, ]) ];
Polyline encoded string
Encode a location using the Encoded Polyline Algorithm Format
// or by polyline $locations = 'enc:gfo}EtohhU';
Make API call
// make API call $results = $elevation->getByLocations($locations);
Get results (Sampled Path Requests)
First of all you have to prepare the path
variable, it can be an array of Location
objects or a polyline string.
Array of Location objects
Using multiple Location objects inside an array
// or by multiple Location objects $path = [ new Location([ LatLngFields::LAT => 39.73915360, LatLngFields::LNG => -104.9847034, ]), // ... more locations new Location([ LatLngFields::LAT => 50.123, LatLngFields::LNG => 99.456, ]) ];
Polyline encoded string
Encode a location using the Encoded Polyline Algorithm Format
// or by polyline $path = 'enc:gfo}EtohhUxD@bAxJmGF';
Make API call
// make API call $samples = 5; // must be int > 0 $results = $elevation->getBySampledPath($path, $samples);
Use results
Results is/are a Biscolab\GoogleMaps\Http\ElevationResultsCollection
object.
First thing you should know how many results there are in your ElevationResultsCollection
using count
method.
$number_of_results = $results->count();
To retrieve the first result you can use the first
method:
$first_result = $results->first();
$first_result
is an instance of ElevationResult
class and has the following methods:
Places API
The Places API allows you to query for place information on a variety of categories, such as: establishments, prominent points of interest, geographic locations, and more. You can search for places either by proximity or a text string (credits: Official Documentation website.
There are 3 types of request:
- Find Place requests
- Nearby Search requests
- Text Search requests
Official Google Place documentation
Initialize Places Object
First of all replace YOUR_API_KEY
with your actual API key.
use Biscolab\GoogleMaps\Api\Places; use Biscolab\GoogleMaps\Enum\GoogleMapsApiConfigFields; $place = new Places([ GoogleMapsApiConfigFields::KEY => 'YOUR_API_KEY' ]);
Find Places requests
This function takes a text input (name, address or phone number) and returns a place.
Using name or address
Search place using the "name" or "address".
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields; // get results by place's name or address $result = $places->findPlaceByText("Museum of Contemporary Art Australia");
findPlaceByText
method accepts 3 arguments
Find further details about request fields (required, types, etc...) here: https://developers.google.com/places/web-service/search#FindPlaceRequests
Using phone number
Search place using the "phone number".
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields; // get results by place's phone number $result = $places->findPlaceByPhoneNumber("+61293744000");
findPlaceByPhoneNumber
method accepts 3 arguments
Find further details about request fields (required, types, etc...) here: https://developers.google.com/places/web-service/search#FindPlaceRequests
Nearby Search requests
This function looks for places within a specified area.
Using location & radius
use Biscolab\GoogleMaps\Object\Location; use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields; $location = new Location([ LatLngFields::LAT => -33.8670522, LatLngFields::LNG => 151.1957362, ]); $radius = 1000; $result = $places->findNearbyPlaceByRadius($location, $radius);
findNearbyPlaceByRadius
method accepts 3 arguments
Rank by distance
use Biscolab\GoogleMaps\Object\Location; use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields; $location = new Location([ LatLngFields::LAT => -33.8670522, LatLngFields::LNG => 151.1957362, ]); // You MUST set at least one of following values $params = [ GoogleMapsRequestFields::KEYWORD => 'a keyword', GoogleMapsRequestFields::NAME => 'name of the place you are looking for', // Biscolab\GoogleMaps\Values\PlaceTypeValues enum class GoogleMapsRequestFields::TYPE => 'Type of the place you are looking for' ]; $result = $places->findNearbyPlaceByDistance($location, $params);
findNearbyPlaceByDistance
method accepts 2 arguments
Find further details about request fields (required, types, etc...) here: https://developers.google.com/places/web-service/search#PlaceSearchRequests
Text Search request
This service returns information about a set of places based on a string.
Search by query
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields; $query = "restaurants in Sydney"; $params = [ ... ]; $result = $places->textSearch($query, $params);
textSearch
method accepts 2 arguments
Find further details about request fields (required, types, etc...) here: https://developers.google.com/places/web-service/search#TextSearchRequests
Place's Details
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields; $place_id = "ChIJN1t_tDeuEmsRUsoyG83frY4"; $params = [ ... ]; $result = $places->details($place_id, $params);
details
method accepts 2 arguments
Find further details about request fields (required, types, etc...) here: https://developers.google.com/places/web-service/details#PlaceDetailsRequests
Use results
Results is/are a Biscolab\GoogleMaps\Http\PlaceResultsCollection
object.
Current page
First thing you should know how many results there are in your PlaceResultsCollection
using count
method.
$number_of_results = $results->count();
To retrieve the first result you can use the first
method:
$first_result = $results->first();
$first_result
is an instance of PlaceResult
class and has the following methods:
Next result page
Results can be paginated. How do you know id a result has more pages?
// getNextPage method checks if $result has "next page" $next_page_result = $result->getNextPage();
Time Zone API
The Time Zone API provides time offset data for locations on the surface of the earth
Official Google TimeZone documentation
Initialize TimeZone Object
First of all replace YOUR_API_KEY
with your actual API key.
use Biscolab\GoogleMaps\Api\TimeZone; use Biscolab\GoogleMaps\Enum\GoogleMapsApiConfigFields; $timezone = new TimeZone([ GoogleMapsApiConfigFields::KEY => 'YOUR_API_KEY' ]);
Get result
Go to complete SDK reference
By location and timestamp
$result = $timezone->get($location, $timestamp, $language);
get
accepts 3 arguments
Use result
Result is an instance of Biscolab\GoogleMaps\Http\Result\TimeZoneResult
class and has the following methods: