A PHP wrapper for the Google Maps Geocoding API v3.

author Justin Stayton
copyright Copyright 2011-2012 by Justin Stayton
license MIT License
link https://developers.google.com/maps/documentation/geocoding/
package GoogleMapsGeocoder
version 2.0.1

 Methods

Constructor.

__construct(string $address, string $format, bool | string $sensor) : \GoogleMapsGeocoder

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

Parameters

$address

string

optional address to geocode

$format

string

optional response format (JSON default)

$sensor

boolstring

optional whether device has location sensor

Returns

Computes a four point bounding box around the specified location.

boundingBox(float | string $latitude, float | string $longitude, int | float | string $mileRange) : array

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.

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.

geocode(bool $https, bool $raw) : string | array | \SimpleXMLElement

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

link https://developers.google.com/maps/documentation/geocoding/#GeocodingResponses

Parameters

$https

bool

whether to make the request over HTTPS

$raw

bool

whether to return the raw (string) response

Returns

stringarray\SimpleXMLElementresponse in requested format

Get the address to geocode.

getAddress() : string

Returns

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/#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/#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/#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/#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/#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/#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/#Viewports

Returns

floatstringsouthwest longitude boundary

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/#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/#ReverseGeocoding

Returns

stringfalsecomma-separated coordinates, or false if not set

Get the longitude to reverse geocode to the closest address.

getLongitude() : float | string

link https://developers.google.com/maps/documentation/geocoding/#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/#RegionCodes

Returns

stringtwo-character, top-level domain (ccTLD)

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

getSensor() : string

Returns

string'true' or 'false'

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

Parameters

$address

string

address to geocode

Returns

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/#Viewports

Parameters

$southwestLatitude

floatstring

southwest latitude boundary

$southwestLongitude

floatstring

southwest longitude boundary

$northeastLatitude

floatstring

northeast latitude boundary

$northeastLongitude

floatstring

northeasy 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/#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/#Viewports

Parameters

$latitude

floatstring

southwest latitude boundary

$longitude

floatstring

southwest longitude boundary

Returns

Set the response format.

setFormat(string $format) : \GoogleMapsGeocoder

Set the language code in which to return results.

setLanguage(string $language) : \GoogleMapsGeocoder

Set the latitude to reverse geocode to the closest address.

setLatitude(float | string $latitude) : \GoogleMapsGeocoder

link https://developers.google.com/maps/documentation/geocoding/#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/#ReverseGeocoding

Parameters

$latitude

floatstring

latitude to reverse geocode

$longitude

floatstring

longitude to reverse geocode

Returns

Set the longitude to reverse geocode to the closest address.

setLongitude(float | string $longitude) : \GoogleMapsGeocoder

link https://developers.google.com/maps/documentation/geocoding/#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/#RegionCodes

Parameters

$region

string

two-character, top-level domain (ccTLD)

Returns

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

setSensor(bool | string $sensor) : \GoogleMapsGeocoder

Parameters

$sensor

boolstring

boolean or 'true'/'false' string

Returns

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

geocodeQueryString() : string

link https://developers.google.com/maps/documentation/geocoding/#GeocodingRequests

Returns

stringencoded query string of the geocode request

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

geocodeUrl(bool $https) : string

link https://developers.google.com/maps/documentation/geocoding/#GeocodingRequests

Parameters

$https

bool

whether to make the request over HTTPS

Returns

stringURL of the geocode request

 Properties

 

Address to geocode.

$address : string

 

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

$boundsNortheastLatitude : float | string

 

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

$boundsNortheastLongitude : float | string

 

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

$boundsSouthwestLatitude : float | string

 

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

$boundsSouthwestLongitude : float | string

 

Response format.

$format : string

 

Language code in which to return results.

$language : string

 

Latitude to reverse geocode to the closest address.

$latitude : float | string

 

Longitude to reverse geocode to the closest address.

$longitude : float | string

 

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

$region : string

 

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

$sensor : string

 Constants

 

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

EQUATOR_LAT_DEGREE_IN_MILES 

 

JSON response format.

FORMAT_JSON 

 

XML response format.

FORMAT_XML 

 

Returned result is approximate.

LOCATION_TYPE_APPROXIMATE 

 

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

LOCATION_TYPE_CENTER 

 

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

LOCATION_TYPE_RANGE 

 

Returned result is a precise street address.

LOCATION_TYPE_ROOFTOP 

 

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

STATUS_INVALID_REQUEST 

 

Geocode was successful, but returned no results.

STATUS_NO_RESULTS 

 

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

STATUS_OVER_LIMIT 

 

Request denied, usually because of missing sensor parameter.

STATUS_REQUEST_DENIED 

 

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

STATUS_SUCCESS 

 

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

TYPE_ADMIN_AREA_1 

 

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

TYPE_ADMIN_AREA_2 

 

A third-order civil entity below country.

TYPE_ADMIN_AREA_3 

 

An airport.

TYPE_AIRPORT 

 

A commonly-used alternative name for the entity.

TYPE_COLLOQUIAL_AREA 

 

A national political entity (country).

TYPE_COUNTRY 

The highest order type returned.

 

A floor of a building address.

TYPE_FLOOR 

 

A major intersection, usually of two major roads.

TYPE_INTERSECTION 

 

An incorporated city or town.

TYPE_LOCALITY 

 

A prominent natural feature.

TYPE_NATURAL_FEATURE 

 

A named neighborhood.

TYPE_NEIGHBORHOOD 

 

A named park.

TYPE_PARK 

 

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

TYPE_POINT_OF_INTEREST 

 

A political entity, usually of some civil administration.

TYPE_POLITICAL 

 

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

TYPE_POSTAL_CODE 

 

A specific postal box.

TYPE_POST_BOX 

 

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

TYPE_PREMISE 

 

A room of a building address.

TYPE_ROOM 

 

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

TYPE_ROUTE 

 

A precise street address.

TYPE_STREET_ADDRESS 

 

A precise street number.

TYPE_STREET_NUMBER 

 

A first-order civil entity below a locality.

TYPE_SUB_LOCALITY 

 

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

TYPE_SUB_PREMISE 

 

HTTP URL of the Google Geocoding API.

URL_HTTP 

 

HTTPS URL of the Google Geocoding API.

URL_HTTPS