Location

Reference for the Location module: geocode and reverse geocode addresses and coordinates through Google, immutable data objects for a coordinate, an address and a combined location, persistence and lookup logging, and map UI components.

This page is a pure reference. For how the framework fits together, see the Guide.

Classes

Location

Static helper for LocationData. Geocode an address to a location, reverse geocode a coordinate, read a location's coordinate or address, convert to and from JSON, and validate a form field holding location JSON.

Coordinate

Location::Coordinate(LocationData $location): LocationCoordinateData

Get the coordinate from a LocationData.

NameTypeDescription
$locationLocationDataLocation data.

Returns LocationCoordinateData The coordinate data.

FromJson

Location::FromJson(stdClass $json): LocationData

Build a LocationData from a decoded JSON object holding a coordinate and an optional address.

NameTypeDescription
$jsonstdClassThe decoded location JSON.

Returns LocationData The location data.

FromJsonOptional

Location::FromJsonOptional(stdClass|null $json): LocationData|null

Build a LocationData from a decoded JSON object, or null when the input is null.

NameTypeDescription
$jsonstdClass|nullThe decoded location JSON, or null.

Returns LocationData|null The location data, or null.

Lookup

Location::Lookup(string $addressString): LocationData|null

Geocode an address string to a LocationData using the Google Geocoding API. Returns null when geocoding fails.

NameTypeDescription
$addressStringstringAddress to geocode.

Returns LocationData|null Location data, or null if geocoding fails.

OptionalAddress

Location::OptionalAddress(LocationData $location): LocationAddressData|null

Get the optional address from a LocationData.

NameTypeDescription
$locationLocationDataLocation data.

Returns LocationAddressData|null The optional address data.

ReverseLookup

Location::ReverseLookup(LocationCoordinateData $coordinate): LocationData|null

Reverse geocode a coordinate to a LocationData using the Google Geocoding API. Returns null when reverse geocoding fails.

NameTypeDescription
$coordinateLocationCoordinateDataCoordinate to reverse geocode.

Returns LocationData|null Location data, or null if reverse geocoding fails.

ToJson

Location::ToJson(LocationData $location): stdClass

Convert a LocationData to a JSON object with its coordinate and optional address.

NameTypeDescription
$locationLocationDataThe location to convert.

Returns stdClass The location as a JSON object.

ValidateFormField

Location::ValidateFormField(): callable

Return an ApiFormValidate-compatible validator for a form field holding location JSON. The returned validator accepts a JSON string or a decoded object and yields a LocationData or an error message.

Returns callable A validator for a location form field.

LocationAddress

JSON conversion helper for LocationAddressData.

FromJson

LocationAddress::FromJson(stdClass $json): LocationAddressData

Build a LocationAddressData from a decoded JSON object.

NameTypeDescription
$jsonstdClassThe decoded address JSON.

Returns LocationAddressData The address data.

FromJsonOptional

LocationAddress::FromJsonOptional(stdClass|null $json): LocationAddressData|null

Build a LocationAddressData from a decoded JSON object, or null when the input is null.

NameTypeDescription
$jsonstdClass|nullThe decoded address JSON, or null.

Returns LocationAddressData|null The address data, or null.

ToJson

LocationAddress::ToJson(LocationAddressData $address): stdClass

Convert a LocationAddressData to a JSON object.

NameTypeDescription
$addressLocationAddressDataThe address to convert.

Returns stdClass The address as a JSON object.

LocationAddressData

Immutable data for a postal address. Fields and constructor only.

Properties

PropertyTypeDescription
$data->stringstringA display string for the address (readonly).
$data->optionalStreetNumberstring|nullStreet number (readonly).
$data->optionalStreetNamestring|nullStreet name or route (readonly).
$data->optionalLocalitystring|nullLocality, such as a town or city (readonly).
$data->optionalAdministrativeAreaLevel1string|nullFirst-level administrative area (readonly).
$data->optionalAdministrativeAreaLevel2string|nullSecond-level administrative area (readonly).
$data->optionalCountrystring|nullCountry (readonly).
$data->optionalPostalCodestring|nullPostal code (readonly).
$data->optionalFormattedAddressstring|nullFull formatted address (readonly).

__construct

new LocationAddressData(string $string, string|null $optionalStreetNumber = null, string|null $optionalStreetName = null, string|null $optionalLocality = null, string|null $optionalAdministrativeAreaLevel1 = null, string|null $optionalAdministrativeAreaLevel2 = null, string|null $optionalCountry = null, string|null $optionalPostalCode = null, string|null $optionalFormattedAddress = null)

Create address data from a display string and optional address components.

NameTypeDescription
$stringstringA display string for the address.
$optionalStreetNumberstring|nullStreet number.
$optionalStreetNamestring|nullStreet name or route.
$optionalLocalitystring|nullLocality, such as a town or city.
$optionalAdministrativeAreaLevel1string|nullFirst-level administrative area.
$optionalAdministrativeAreaLevel2string|nullSecond-level administrative area.
$optionalCountrystring|nullCountry.
$optionalPostalCodestring|nullPostal code.
$optionalFormattedAddressstring|nullFull formatted address.

Returns LocationAddressData

LocationAddressDatabase

Store and read address rows in the location_address table.

Add

LocationAddressDatabase::Add(LocationAddressData $address): int

Insert an address row and return its new id.

NameTypeDescription
$addressLocationAddressDataThe address to store.

Returns int The inserted address id.

Get

LocationAddressDatabase::Get(int $addressId): LocationAddressData

Read a single address by id.

NameTypeDescription
$addressIdintThe address id.

Returns LocationAddressData The address data.

GetListByIDs

LocationAddressDatabase::GetListByIDs(array $addressIds): array

Read many addresses at once, returning a map of address id to address data.

NameTypeDescription
$addressIdsarrayThe address ids to read.

Returns array A map of address id to address data.

GetOptional

LocationAddressDatabase::GetOptional(int|null $addressId): LocationAddressData|null

Read a single address by id, or null when the id is null or no row is found.

NameTypeDescription
$addressIdint|nullThe address id, or null.

Returns LocationAddressData|null The address data, or null.

LocationCoordinate

JSON conversion helper for LocationCoordinateData.

Functions

FromJson

LocationCoordinate::FromJson(stdClass $json): LocationCoordinateData

Build a LocationCoordinateData from a decoded JSON object holding latitude and longitude.

NameTypeDescription
$jsonstdClassThe decoded coordinate JSON.

Returns LocationCoordinateData The coordinate data.

ToJson

LocationCoordinate::ToJson(LocationCoordinateData $coordinate): stdClass

Convert a LocationCoordinateData to a JSON object with latitude and longitude.

NameTypeDescription
$coordinateLocationCoordinateDataThe coordinate to convert.

Returns stdClass The coordinate as a JSON object.

LocationCoordinateData

Immutable latitude and longitude pair. Fields and constructor only.

Properties

PropertyTypeDescription
$data->latitudefloatLatitude, between -90 and 90 (readonly).
$data->longitudefloatLongitude, between -180 and 180 (readonly).

__construct

new LocationCoordinateData(float $latitude = 0, float $longitude = 0)

Create a coordinate from a latitude and longitude.

NameTypeDescription
$latitudefloatLatitude, between -90 and 90.
$longitudefloatLongitude, between -180 and 180.

Returns LocationCoordinateData

LocationData

Immutable coordinate with an optional address. Fields and constructor only.

Properties

PropertyTypeDescription
$data->coordinateLocationCoordinateDataThe location coordinate (readonly).
$data->optionalAddressLocationAddressData|nullThe optional address for the location (readonly).

__construct

new LocationData(LocationCoordinateData $coordinate = new LocationCoordinateData(), LocationAddressData|null $optionalAddress = null)

Create a location from a coordinate and an optional address.

NameTypeDescription
$coordinateLocationCoordinateDataThe location coordinate.
$optionalAddressLocationAddressData|nullThe optional address for the location.

Returns LocationData

LocationDatabase

Remote lookups via Google APIs: reverse geocode a coordinate, forward geocode an address, search addresses, and measure the driving journey between two points. Forward geocoding is logged to the lookup log.

AddressFromCoordinates

LocationDatabase::AddressFromCoordinates(LocationCoordinateData $coordinate): LocationAddressData|null

Reverse geocode a coordinate to an address via the Google Geocoding API. Returns null when the lookup fails.

NameTypeDescription
$coordinateLocationCoordinateDataCoordinate to reverse geocode.

Returns LocationAddressData|null Address data, or null if the lookup fails.

AddressSearch

LocationDatabase::AddressSearch(string $query, int $maxResults = 5): array

Search for addresses matching a query string via the Google Geocoding API, returning up to the requested number of results. Queries shorter than three characters return an empty list.

NameTypeDescription
$querystringSearch query string.
$maxResultsintMaximum number of results to return.

Returns array An array of matching LocationData.

AddressToCoordinates

LocationDatabase::AddressToCoordinates(string $address): LocationData|null

Forward geocode an address string to a LocationData with coordinates via the Google Geocoding API, logging the lookup. Returns null when geocoding fails.

NameTypeDescription
$addressstringAddress to geocode.

Returns LocationData|null Location data, or null if geocoding fails.

FromCoordinates

LocationDatabase::FromCoordinates(LocationCoordinateData $coordinate): LocationData|null

Create a LocationData from a coordinate by reverse geocoding its address.

NameTypeDescription
$coordinateLocationCoordinateDataCoordinate to reverse geocode.

Returns LocationData|null Location data with an address.

GetDistanceAndDuration

LocationDatabase::GetDistanceAndDuration(float $originLat, float $originLng, float $destLat, float $destLng): array

Get the driving distance and duration between two coordinates via the Google Routes computeRouteMatrix API. Throws when no route exists or the request fails.

NameTypeDescription
$originLatfloatOrigin latitude.
$originLngfloatOrigin longitude.
$destLatfloatDestination latitude.
$destLngfloatDestination longitude.

Returns array A pair of journey length in meters and journey time in seconds.

LocationLookup

Geocoding through the Google Geocoding API: reverse geocode a coordinate, forward geocode an address, and search addresses. Forward geocoding is logged to the lookup log.

AddressFromCoordinates

LocationLookup::AddressFromCoordinates(LocationCoordinateData $coordinate): LocationAddressData|null

Reverse geocode a coordinate to an address via the Google Geocoding API. Returns null when the lookup fails.

NameTypeDescription
$coordinateLocationCoordinateDataCoordinate to reverse geocode.

Returns LocationAddressData|null Address data, or null if the lookup fails.

AddressSearch

LocationLookup::AddressSearch(string $query, int $maxResults = 5): array

Search for addresses matching a query string via the Google Geocoding API, returning up to the requested number of results. Queries shorter than three characters return an empty list.

NameTypeDescription
$querystringSearch query string.
$maxResultsintMaximum number of results to return.

Returns array An array of matching LocationData.

AddressToCoordinates

LocationLookup::AddressToCoordinates(string $address): LocationData|null

Forward geocode an address string to a LocationData with coordinates via the Google Geocoding API, logging the lookup. Returns null when geocoding fails.

NameTypeDescription
$addressstringAddress to geocode.

Returns LocationData|null Location data, or null if geocoding fails.

fromAddressString

LocationLookup::fromAddressString(string $addressString): LocationData|null

Forward geocode an address string to a LocationData. Delegates to AddressToCoordinates.

NameTypeDescription
$addressStringstringAddress to geocode.

Returns LocationData|null Location data, or null if geocoding fails.

fromCoordinates

LocationLookup::fromCoordinates(LocationCoordinateData $coordinate): LocationData|null

Build a LocationData from a coordinate by reverse geocoding its address.

NameTypeDescription
$coordinateLocationCoordinateDataCoordinate to reverse geocode.

Returns LocationData|null Location data with an address.

LocationLookupDatabase

Store and read lookup log entries in the location_lookup_log table. Storing an entry also stores the associated address and, when enabled, the raw response JSON.

Functions

Add

LocationLookupDatabase::Add(LocationData $location, string|null $query = null, string|null $type = null, string|null $responseJson = null): int

Insert a lookup log entry and return its new id. The raw response JSON is stored only when response logging is enabled.

NameTypeDescription
$locationLocationDataThe location data.
$querystring|nullThe search query, for search or geocode operations.
$typestring|nullThe lookup type, such as geocode, reverse or search.
$responseJsonstring|nullThe raw response JSON, stored only when logging is enabled.

Returns int The inserted log id.

Get

LocationLookupDatabase::Get(int $logId): LocationData

Read a single lookup log entry by id as a LocationData.

NameTypeDescription
$logIdintThe log id.

Returns LocationData The location data.

GetListByIDs

LocationLookupDatabase::GetListByIDs(array $logIds): array

Read many lookup log entries at once, returning a map of log id to LocationData.

NameTypeDescription
$logIdsarrayThe log ids to read.

Returns array A map of log id to LocationData.

LocationMarkerData

Immutable data for a map marker. Fields and constructor only.

Properties

PropertyTypeDescription
$data->coordinateLocationCoordinateDataThe marker coordinate (readonly).
$data->titlestring|nullThe marker title (readonly).
$data->snippetstring|nullThe marker snippet text (readonly).

__construct

new LocationMarkerData(LocationCoordinateData $coordinate, string|null $title = null, string|null $snippet = null)

Create marker data from a coordinate and optional title and snippet.

NameTypeDescription
$coordinateLocationCoordinateDataThe marker coordinate.
$titlestring|nullThe marker title.
$snippetstring|nullThe marker snippet text.

Returns LocationMarkerData

LocationUi

Map UI components backed by Google Maps: an interactive map that gathers markers, a static map image, a marker registered within a map, and an address input with Places autocomplete.

Input

LocationUi::Input(string $inputName, string $label, string|null $initialText = null, string|null $optionalStateName = null, array $optionalStateParameters = []): void

Render a location input field with Google Places autocomplete. The chosen place is stored as location JSON in a hidden field.

NameTypeDescription
$inputNamestringThe name of the input field.
$labelstringThe label for the input field.
$initialTextstring|nullOptional initial text to prefill.
$optionalStateNamestring|nullOptional reactive state name for a selection callback.
$optionalStateParametersarrayOptional state parameters for a selection callback.

Returns void

Map

LocationUi::Map(LocationCoordinateData $coordinate, float|null $height = null, string|null $mapId = null, callable|null $content = null): void

Render an interactive Google map centred on a coordinate. Markers added by the content callback are collected and placed on the map.

NameTypeDescription
$coordinateLocationCoordinateDataCentre coordinate of the map.
$heightfloat|nullOptional height in pixels.
$mapIdstring|nullOptional unique id for the map container.
$contentcallable|nullCallback that renders content and gathers markers.

Returns void

Marker

LocationUi::Marker(string $markerId, LocationCoordinateData $coordinate, string|null $title = null, string|null $snippet = null): void

Register a marker at a coordinate within a surrounding Map content callback.

NameTypeDescription
$markerIdstringAn id for the marker.
$coordinateLocationCoordinateDataThe marker coordinate.
$titlestring|nullThe marker title.
$snippetstring|nullThe marker snippet text.

Returns void

StaticImage

LocationUi::StaticImage(LocationData $location, int $width, int $height, string|null $alt = null): void

Render a static Google map image for a location at the given size.

NameTypeDescription
$locationLocationDataThe location to show.
$widthintImage width in pixels.
$heightintImage height in pixels.
$altstring|nullOptional alternative text for the image.

Returns void