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.
Go to complete reference or read documentation
- Geocoding ☑️
- Elevation ☑️
- Places ☑️
- Time Zone ☑️
- Directions (soon)
- Distance Matrix (soon)
- Road (soon)
- Geolocation (not scheduled)
You can install the package via composer:
composer require biscolab/google-maps-php-sdk
Watch the examples
This provide simple functions to work with Google Maps APIs. You can find further informations in Google Maps Platform Documentation
You can find complete API references
- Directions (soon)
- Distance Matrix (soon)
- Road (soon)
- Geolocation (not scheduled)
PHP 7.1 or greater
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...)
The Geocoding API is a service that provides geocoding and reverse geocoding of addresses.
Official Google Geocoding documentation
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'
]);
You have 3 different ways to retrieve data of your place!
Go to complete SDK reference
getByAddress
accept following arguments:
Name | Required | Type | Description |
---|---|---|---|
address | yes | string |
The street address or plus code that you want to geocode |
region | no | string |
The region code, specified as a ccTLD ("top-level domain") two-character value. More info here |
$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...');
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
getByPlaceId
accept as parameter the address the place ID.
$results = $geocoding->getByPlaceId('YOUR_PLACE_ID');
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:
Method name | Return Type |
---|---|
getAddressComponents() |
Address |
getFormattedAddress() |
string |
getGeometry() |
Geometry |
getPlaceId() |
string |
getTypes() |
array |
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
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'
]);
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.
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,
]);
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,
])
];
Encode a location using the Encoded Polyline Algorithm Format
// or by polyline
$locations = 'enc:gfo}EtohhU';
// make API call
$results = $elevation->getByLocations($locations);
First of all you have to prepare the path
variable, it can be an array of Location
objects or a polyline string.
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,
])
];
Encode a location using the Encoded Polyline Algorithm Format
// or by polyline
$path = 'enc:gfo}EtohhUxD@bAxJmGF';
// make API call
$samples = 5; // must be int > 0
$results = $elevation->getBySampledPath($path, $samples);
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:
Method name | Return Type |
---|---|
getLocation() |
Location |
getElevation() |
float |
getResolution() |
float |
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
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'
]);
This function takes a text input (name, address or phone number) and returns a place.
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
Name | Type | Description | Default |
---|---|---|---|
$query |
string |
The address (or the name) specifying which place to search for | Required |
$params |
array |
The list of search params to add to API request | [] |
$fields |
array |
The fields specifying the types of place data to return | [] |
Find further details about request fields (required, types, etc...) here: https://developers.google.com/places/web-service/search#FindPlaceRequests
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
Name | Type | Description | Default |
---|---|---|---|
$number |
string |
The phone number specifying which place to search for | Required |
$params |
array |
The list of search params to add to API request | [] |
$fields |
array |
The fields specifying the types of place data to return | [] |
Find further details about request fields (required, types, etc...) here: https://developers.google.com/places/web-service/search#FindPlaceRequests
This function looks for places within a specified area.
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
Name | Type | Description | Default |
---|---|---|---|
$location |
Location |
must be instance of Biscolab\GoogleMaps\Object\Location class |
Required |
$radius |
int |
defines the distance in meters. Maximum allowed value is 50000. | Required |
$params |
array |
additional parameters to add to API call | [] |
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
Name | Type | Description | Default |
---|---|---|---|
$location |
Location |
must be instance of Biscolab\GoogleMaps\Object\Location class |
Required |
$params |
array |
additional parameters to add to API call. You MUST set at least one of following values: keyword , name , type |
[] |
Find further details about request fields (required, types, etc...) here: https://developers.google.com/places/web-service/search#PlaceSearchRequests
This service returns information about a set of places based on a string.
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields;
$query = "restaurants in Sydney";
$params = [
...
];
$result = $places->textSearch($query, $params);
textSearch
method accepts 2 arguments
Name | Type | Description | Default |
---|---|---|---|
$query |
string |
The text string on which to search, for example: "restaurant" or "123 Main Street". | Required |
$params |
array |
additional parameters to add to API call | [] |
Find further details about request fields (required, types, etc...) here: https://developers.google.com/places/web-service/search#TextSearchRequests
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields;
$place_id = "ChIJN1t_tDeuEmsRUsoyG83frY4";
$params = [
...
];
$result = $places->details($place_id, $params);
details
method accepts 2 arguments
Name | Type | Description | Default |
---|---|---|---|
$place_id |
string |
A textual identifier that uniquely identifies a place, returned from a Place Search. | Required |
$params |
array |
additional parameters to add to API call | [] |
Find further details about request fields (required, types, etc...) here: https://developers.google.com/places/web-service/details#PlaceDetailsRequests
Results is/are a Biscolab\GoogleMaps\Http\PlaceResultsCollection
object.
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:
Method name | Return Type |
---|---|
getPhotos() |
PhotoCollection |
getGeometry() |
Geometry |
getFormattedAddress() |
string |
getName() |
string |
getIcon() |
string |
getId() |
string |
getPlaceId() |
string |
getReference() |
string |
getVicinity() |
string |
getTypes() |
array |
getOpeningHours() |
array |
getPriceLevel() |
int |
getRating() |
float |
getPermanentlyClose() |
bool |
getPlusCode() |
array |
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();
The Time Zone API provides time offset data for locations on the surface of the earth
Official Google TimeZone documentation
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'
]);
Go to complete SDK reference
$result = $timezone->get($location, $timestamp, $language);
get
accepts 3 arguments
Name | Requested | Type | Description | |
---|---|---|---|---|
$location |
Yes | Biscolab\GoogleMaps\Object\Location | Represents the location to look up | |
$timestamp |
Yes | integer | The Time Zone API uses the $timestamp to determine whether or not Daylight Savings should be applied, based on the time zone of the $location |
|
$language |
No | string | The language in which to return results |
Result is an instance of Biscolab\GoogleMaps\Http\Result\TimeZoneResult
class and has the following methods:
Method name | Return Type |
---|---|
getDdstOffset() |
int |
getRawOffset() |
int |
getTimeZoneId() |
string |
getTimeZoneName() |
string |