A PHP wrapper for the Google Maps Geocoding API v3.

author Justin Stayton
copyright Copyright 2015 by Justin Stayton
license MIT
link https://developers.google.com/maps/documentation/geocoding/intro
package GoogleMapsGeocoder
version 2.4.0

 Methods

Constructor. The request is not executed until `geocode()` is called.

__construct(string $address = null, string $format = self::FORMAT_JSON, bool|string $sensor = false) : \GoogleMapsGeocoder

Parameters

$address

string

optional address to geocode

$format

string

optional response format (JSON default)

$sensor

boolstring

deprecated as of v2.3.0

Returns

Computes a four point bounding box around the specified location. This can then be used to find all locations within an X-mile range of a central location. A bounding box is much easier and faster to compute than a bounding radius.

boundingBox(float|string $latitude, float|string $longitude, int|float|string $mileRange) : array
Static
The returned array contains two keys: 'lat' and 'lon'. Each of these contains another array with two keys: 'max' and 'min'. Four points are returned in total.

Parameters

$latitude

floatstring

to draw the bounding box around

$longitude

floatstring

to draw the bounding box around

$mileRange

intfloatstring

mile range around point

Returns

array'lat' and 'lon' 'min' and 'max' points

Execute the geocoding request. The return type is based on the requested format: associative array if JSON, SimpleXMLElement object if XML.

geocode(bool $https = false, bool $raw = false, resource $context = null) : string | array | \SimpleXMLElement
link https://developers.google.com/maps/documentation/geocoding/intro#GeocodingResponses

Parameters

$https

bool

whether to make the request over HTTPS

$raw

bool

whether to return the raw (string) response

$context

resource

stream context from stream_context_create()

Returns

stringarray\SimpleXMLElementresponse in requested format

Get the address to geocode.

getAddress() : string

Get the API key to authenticate with.

getApiKey() : string

Get the bounding box coordinates within which to bias geocode results in comma-separated, pipe-delimited format.

getBounds() : string | false
link https://developers.google.com/maps/documentation/geocoding/intro#Viewports

Returns

stringfalsecomma-separated, pipe-delimited coordinates, or false if not set

Get the northeast coordinates of the bounding box within which to bias geocode results in comma-separated format.

getBoundsNortheast() : string | false
link https://developers.google.com/maps/documentation/geocoding/intro#Viewports

Returns

stringfalsecomma-separated coordinates, or false if not set

Get the northeast latitude of the bounding box within which to bias geocode results.

getBoundsNortheastLatitude() : float | string
link https://developers.google.com/maps/documentation/geocoding/intro#Viewports

Returns

floatstringnortheast latitude boundary

Get the northeast longitude of the bounding box within which to bias geocode results.

getBoundsNortheastLongitude() : float | string
link https://developers.google.com/maps/documentation/geocoding/intro#Viewports

Returns

floatstringnortheast longitude boundary

Get the southwest coordinates of the bounding box within which to bias geocode results in comma-separated format.

getBoundsSouthwest() : string | false
link https://developers.google.com/maps/documentation/geocoding/intro#Viewports

Returns

stringfalsecomma-separated coordinates, or false if not set

Get the southwest latitude of the bounding box within which to bias geocode results.

getBoundsSouthwestLatitude() : float | string
link https://developers.google.com/maps/documentation/geocoding/intro#Viewports

Returns

floatstringsouthwest latitude boundary

Get the southwest longitude of the bounding box within which to bias geocode results.

getBoundsSouthwestLongitude() : float | string
link https://developers.google.com/maps/documentation/geocoding/intro#Viewports

Returns

floatstringsouthwest longitude boundary

Get the client ID for Business clients.

getClientId() : string

Get the response format.

getFormat() : string

Get the language code in which to return results.

getLanguage() : string

Get the latitude to reverse geocode to the closest address.

getLatitude() : float | string
link https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding

Returns

floatstringlatitude to reverse geocode

Get the latitude/longitude to reverse geocode to the closest address in comma-separated format.

getLatitudeLongitude() : string | false
link https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding

Returns

stringfalsecomma-separated coordinates, or false if not set

Get the location type(s) to restrict results to.

getLocationType() : array

Get the location type(s) to restrict results to separated by a pipe (|).

getLocationTypeFormatted() : string
link https://developers.google.com/maps/documentation/geocoding/intro#reverse-restricted

Returns

stringlocation type(s) separated by a pipe (|)

Get the longitude to reverse geocode to the closest address.

getLongitude() : float | string
link https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding

Returns

floatstringlongitude to reverse geocode

Get the two-character, top-level domain (ccTLD) within which to bias geocode results.

getRegion() : string
link https://developers.google.com/maps/documentation/geocoding/intro#RegionCodes

Returns

stringtwo-character, top-level domain (ccTLD)

Get the address type(s) to restrict results to.

getResultType() : array

Get the address type(s) to restrict results to separated by a pipe (|).

getResultTypeFormatted() : string
link https://developers.google.com/maps/documentation/geocoding/intro#reverse-restricted

Returns

stringaddress type(s) separated by a pipe (|)

Get whether the request is from a device with a location sensor.

getSensor() : string
deprecated 2.3.0 no longer required by the Google Maps API
link https://developers.google.com/maps/documentation/geocoding/intro#Sensor

Returns

string'true' or 'false'

Get the cryptographic signing key for Business clients.

getSigningKey() : string

Whether the request is for a Business client.

isBusinessClient() : bool

Returns

boolwhether the request is for a Business client

Whether the response format is JSON.

isFormatJson() : bool

Whether the response format is XML.

isFormatXml() : bool

Set the address to geocode.

setAddress(string $address) : \GoogleMapsGeocoder

Set the API key to authenticate with.

setApiKey(string $apiKey) : \GoogleMapsGeocoder

Set the bounding box coordinates within which to bias geocode results.

setBounds(float|string $southwestLatitude, float|string $southwestLongitude, float|string $northeastLatitude, float|string $northeastLongitude) : \GoogleMapsGeocoder
link https://developers.google.com/maps/documentation/geocoding/intro#Viewports

Parameters

$southwestLatitude

floatstring

southwest latitude boundary

$southwestLongitude

floatstring

southwest longitude boundary

$northeastLatitude

floatstring

northeast latitude boundary

$northeastLongitude

floatstring

northeast longitude boundary

Returns

Set the northeast coordinates of the bounding box within which to bias geocode results.

setBoundsNortheast(float|string $latitude, float|string $longitude) : \GoogleMapsGeocoder
link https://developers.google.com/maps/documentation/geocoding/intro#Viewports

Parameters

$latitude

floatstring

northeast latitude boundary

$longitude

floatstring

northeast longitude boundary

Returns

Set the southwest coordinates of the bounding box within which to bias geocode results.

setBoundsSouthwest(float|string $latitude, float|string $longitude) : \GoogleMapsGeocoder
link https://developers.google.com/maps/documentation/geocoding/intro#Viewports

Parameters

$latitude

floatstring

southwest latitude boundary

$longitude

floatstring

southwest longitude boundary

Returns

Set the client ID for Business clients.

setClientId(string $clientId) : \GoogleMapsGeocoder

Set the response format.

setFormat(string $format) : \GoogleMapsGeocoder

Set the language code in which to return results.

setLanguage(string $language) : \GoogleMapsGeocoder
link https://developers.google.com/maps/faq#languagesupport

Parameters

$language

string

language code

Returns

Set the latitude to reverse geocode to the closest address.

setLatitude(float|string $latitude) : \GoogleMapsGeocoder
link https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding

Parameters

$latitude

floatstring

latitude to reverse geocode

Returns

Set the latitude/longitude to reverse geocode to the closest address.

setLatitudeLongitude(float|string $latitude, float|string $longitude) : \GoogleMapsGeocoder
link https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding

Parameters

$latitude

floatstring

latitude to reverse geocode

$longitude

floatstring

longitude to reverse geocode

Returns

Set the location type(s) to restrict results to.

setLocationType(string|array $locationType) : \GoogleMapsGeocoder
link https://developers.google.com/maps/documentation/geocoding/intro#reverse-restricted

Parameters

$locationType

stringarray

location type(s)

Returns

Set the longitude to reverse geocode to the closest address.

setLongitude(float|string $longitude) : \GoogleMapsGeocoder
link https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding

Parameters

$longitude

floatstring

longitude to reverse geocode

Returns

Set the two-character, top-level domain (ccTLD) within which to bias geocode results.

setRegion(string $region) : \GoogleMapsGeocoder
link https://developers.google.com/maps/documentation/geocoding/intro#RegionCodes

Parameters

$region

string

two-character, top-level domain (ccTLD)

Returns

Set the address type(s) to restrict results to.

setResultType(string|array $resultType) : \GoogleMapsGeocoder

Set whether the request is from a device with a location sensor.

setSensor(bool|string $sensor) : \GoogleMapsGeocoder
deprecated 2.3.0 no longer required by the Google Maps API
link https://developers.google.com/maps/documentation/geocoding/intro#Sensor

Parameters

$sensor

boolstring

boolean or 'true'/'false' string

Returns

Set the cryptographic signing key for Business clients.

setSigningKey(string $signingKey) : \GoogleMapsGeocoder

Decode a Base64 string that uses only URL safe characters.

base64DecodeUrlSafe(string $value) : string
Static

Parameters

$value

string

value to decode

Returns

stringdecoded value

Encode a string with Base64 using only URL safe characters.

base64EncodeUrlSafe(string $value) : string
Static

Parameters

$value

string

value to encode

Returns

stringencoded value

Generate the signature for a Business client geocode request.

generateSignature(string $pathQueryString) : string
link https://developers.google.com/maps/documentation/business/webservices/auth#digital_signatures

Parameters

$pathQueryString

string

path and query string of the request

Returns

stringBase64 encoded signature that's URL safe

Build the query string with all set parameters of the geocode request.

geocodeQueryString() : string
link https://developers.google.com/maps/documentation/geocoding/intro#GeocodingRequests

Returns

stringencoded query string of the geocode request

Build the URL (with query string) of the geocode request.

geocodeUrl(bool $https = false) : string
link https://developers.google.com/maps/documentation/geocoding/intro#GeocodingRequests

Parameters

$https

bool

whether to make the request over HTTPS

Returns

stringURL of the geocode request

 Properties

 

Address to geocode.

$address : string

Default

 

API key to authenticate with.

$apiKey : string

Default

 

Northeast latitude of the bounding box within which to bias geocode results.

$boundsNortheastLatitude : float | string

Default

 

Northeast longitude of the bounding box within which to bias geocode results.

$boundsNortheastLongitude : float | string

Default

 

Southwest latitude of the bounding box within which to bias geocode results.

$boundsSouthwestLatitude : float | string

Default

 

Southwest longitude of the bounding box within which to bias geocode results.

$boundsSouthwestLongitude : float | string

Default

 

Client ID for Business clients.

$clientId : string

Default

 

Response format.

$format : string

Default

 

Language code in which to return results.

$language : string

Default

 

Latitude to reverse geocode to the closest address.

$latitude : float | string

Default

 

Location type(s) to restrict results to.

$locationType : array

Default

array()
 

Longitude to reverse geocode to the closest address.

$longitude : float | string

Default

 

Two-character, top-level domain (ccTLD) within which to bias geocode results.

$region : string

Default

 

Address type(s) to restrict results to.

$resultType : array

Default

array()
 

Whether the request is from a device with a location sensor.

$sensor : string

Default

deprecated 2.3.0 no longer required by the Google Maps API
 

Cryptographic signing key for Business clients.

$signingKey : string

Default

 Constants

 

Helps calculate a more realistic bounding box by taking into account the curvature of the earth's surface.

EQUATOR_LAT_DEGREE_IN_MILES = 69.172 
 

JSON response format.

FORMAT_JSON = "json" 
 

XML response format.

FORMAT_XML = "xml" 
 

Returned result is approximate.

LOCATION_TYPE_APPROXIMATE = "APPROXIMATE" 
 

Returned result is the geometric center of a polyline (i.e., a street) or polygon (i.e., a region).

LOCATION_TYPE_CENTER = "GEOMETRIC_CENTER" 
 

Returned result is between two precise points (usually on a road).

LOCATION_TYPE_RANGE = "RANGE_INTERPOLATED" 
 

Returned result is a precise street address.

LOCATION_TYPE_ROOFTOP = "ROOFTOP" 
 

Invalid request, usually because of missing parameter that's required.

STATUS_INVALID_REQUEST = "INVALID_REQUEST" 
 

Geocode was successful, but returned no results.

STATUS_NO_RESULTS = "ZERO_RESULTS" 
 

Over limit of 2,500 (100,000 if premier) requests per day.

STATUS_OVER_LIMIT = "OVER_QUERY_LIMIT" 
 

Request denied, usually because of missing sensor parameter.

STATUS_REQUEST_DENIED = "REQUEST_DENIED" 
 

No errors occurred, the address was successfully parsed and at least one geocode was returned.

STATUS_SUCCESS = "OK" 
 

Unnown server error. May succeed if tried again.

STATUS_UNKNOWN_ERROR = "UNKNOWN_ERROR" 
 

A first-order civil entity below country (states within the US).

TYPE_ADMIN_AREA_1 = "administrative_area_level_1" 
 

A second-order civil entity below country (counties within the US).

TYPE_ADMIN_AREA_2 = "administrative_area_level_2" 
 

A third-order civil entity below country.

TYPE_ADMIN_AREA_3 = "administrative_area_level_3" 
 

A fourth-order civil entity below country.

TYPE_ADMIN_AREA_4 = "administrative_area_level_4" 
 

A fifth-order civil entity below country.

TYPE_ADMIN_AREA_5 = "administrative_area_level_5" 
 

An airport.

TYPE_AIRPORT = "airport" 
 

A bus stop.

TYPE_BUS_STATION = "bus_station" 
 

A commonly-used alternative name for the entity.

TYPE_COLLOQUIAL_AREA = "colloquial_area" 
 

A national political entity (country). The highest order type returned.

TYPE_COUNTRY = "country" 
 

A place that has not yet been categorized.

TYPE_ESTABLISHMENT = "establishment" 
 

A floor of a building address.

TYPE_FLOOR = "floor" 
 

A major intersection, usually of two major roads.

TYPE_INTERSECTION = "intersection" 
 

An incorporated city or town.

TYPE_LOCALITY = "locality" 
 

A prominent natural feature.

TYPE_NATURAL_FEATURE = "natural_feature" 
 

A named neighborhood.

TYPE_NEIGHBORHOOD = "neighborhood" 
 

A named park.

TYPE_PARK = "park" 
 

A parking lot or parking structure.

TYPE_PARKING = "parking" 
 

A named point of interest that doesn't fit within another category.

TYPE_POINT_OF_INTEREST = "point_of_interest" 
 

A political entity, usually of some civil administration.

TYPE_POLITICAL = "political" 
 

A postal code as used to address mail within the country.

TYPE_POSTAL_CODE = "postal_code" 
 

A grouping of geographic areas used for mailing addresses in some countries.

TYPE_POSTAL_TOWN = "postal_town" 
 

A specific postal box.

TYPE_POST_BOX = "post_box" 
 

A named location, usually a building or collection of buildings.

TYPE_PREMISE = "premise" 
 

A room of a building address.

TYPE_ROOM = "room" 
 

A named route (such as "US 101").

TYPE_ROUTE = "route" 
 

A precise street address.

TYPE_STREET_ADDRESS = "street_address" 
 

A precise street number.

TYPE_STREET_NUMBER = "street_number" 
 

A first-order civil entity below a locality.

TYPE_SUB_LOCALITY = "sublocality" 
 

A first-order entity below a named location, usually a single building within a collection of building with a common name.

TYPE_SUB_PREMISE = "subpremise" 
 

A train stop.

TYPE_TRAIN_STATION = "train_station" 
 

A public transit stop.

TYPE_TRANSIT_STATION = "transit_station" 
 

A specific type of Japanese locality.

TYPE_WARD = "ward" 
 

Domain portion of the Google Geocoding API URL.

URL_DOMAIN = "maps.googleapis.com" 
 

HTTP URL of the Google Geocoding API.

URL_HTTP = "http://maps.googleapis.com/maps/api/geocode/" 
 

HTTPS URL of the Google Geocoding API.

URL_HTTPS = "https://maps.googleapis.com/maps/api/geocode/" 
 

Path portion of the Google Geocoding API URL.

URL_PATH = "/maps/api/geocode/"