Skip to content

Geo & Location

Distance, lat/long and address helpers.

8 functions. All require apis/main_webservices.php to be included.


On this page: Geo_RoadDistanceFromAddress ยท Geo_RoadDistanceFromLatLon ยท Geo_latlon ยท geoDistance ยท geoTrend ยท getAddressFromLatLon ยท getDistance ยท getLnt


Geo_RoadDistanceFromAddress()

Prototype

Geo_RoadDistanceFromAddress($origin,$destination)

Description

  • Computes the road distance between two postal addresses.
  • Geocodes both addresses, then routes between them.

Parameters

  • $origin โ€” Origin Address
  • $destination โ€” Destination Address

Returns

  • Object โ€” road distance (and routing details) between the addresses.

Return example

$d = Geo_RoadDistanceFromAddress('Surat','Vadodara');
// $d->distance = '149 km'

Usage example

$distance = Geo_RoadDistanceFromAddress('Surat, India','Indore, India');
returns: array('km' => 1000, 'mi' => 700, 'duration' => '3 Hours 5 minutes');

:material-file-code: Source: apis/core2_a.php


Geo_RoadDistanceFromLatLon()

Prototype

Geo_RoadDistanceFromLatLon($lat1, $lon1, $lat2, $lon2)

Description

  • Computes the road (routed) distance between two lat/long points.
  • Unlike geoDistance(), this follows roads rather than a straight line.

Parameters

  • $lat1 โ€” latitude1
  • $lon1 โ€” longitude1
  • $lat2 โ€” latitude2
  • $lon2 โ€” longitude2

Returns

  • Object โ€” road distance (and related routing info) between the two points.

Return example

$d = Geo_RoadDistanceFromLatLon(21.17,72.83, 21.20,72.85);
// $d->distance = '4.2 km'

Usage example

$distance = Geo_RoadDistanceFromLatLon(....)
returns: array('km' => 1000, 'mi' => 700, 'duration' => '3 Hours 5 minutes');

:material-file-code: Source: apis/core2_a.php


Geo_latlon()

Prototype

Geo_latlon($address)

Description

  • Resolves the latitude & longitude of a given address (geocoding).
  • Use before distance/mapping operations that need coordinates.

Parameters

  • $address โ€” Address of point

Returns

  • Object โ€” the latitude and longitude of the address.

Return example

$p = Geo_latlon('Ring Road, Surat');
// $p->lat = 21.18, $p->lon = 72.83

Usage example

$distance = Geo_latlon('Surat, India');
returns: array('lat' => 21.714, 'lon' => 72.2213, 'response' => 'Ok');

:material-file-code: Source: apis/core2_a.php


geoDistance()

Prototype

GeoDistance($lat1, $lon1, $lat2, $lon2)

Description

  • Computes the straight-line distance between two lat/long points, in metres.
  • Use for proximity checks (e.g. attendance within site radius).

Parameters

  • $lat1 โ€” latitude of point1
  • $lon1 โ€” longitude of point1
  • $lat2 โ€” latitude of point2
  • $lon2 โ€” longitude of point2

Returns

  • Number โ€” distance between the points in metres.

Return example

$m = GeoDistance(21.17, 72.83, 21.20, 72.85);
// e.g. 3650  (metres)

Usage example

$distance = GeoDistance( 72.8311, 21.1702, 73.1812, 22.3072 )
returns: distance between Surat & Vadodara in meters.

:material-file-code: Source: apis/core2_a.php


geoTrend()

Prototype

geoTrend(array( ['id' => 1, 'distance' => 100], ['id' => 2, 'distance' => 110], ['id' => 3, 'distance' => 112] ...))

Description

  • Analyses a series of distance/value points and returns a trend summary (increasing/decreasing/stable).
  • Feed an array of {id, distance} points; get back direction plus min/max.

Parameters

  • $array โ€” array of distances

Returns

  • Array โ€” ['trend'=>1|-1|0, 'desc'=>'increasing'|'decreasing'|'stable', 'min'=>.., 'max'=>..].

Return example

$t = geoTrend([['id'=>1,'distance'=>100],['id'=>2,'distance'=>112]]);
// ['trend'=>1,'desc'=>'increasing','min'=>100,'max'=>112]

Usage example

$trend = geoTrend( array(100,120,130,140,150) )
returns: array info of the trend. [ 'trend' => 1, 'desc' => 'increasing', 'min' => ['id' => 1, 'distance' => 100], 'max' => ['id' => 3, 'distance' => 112]

:material-file-code: Source: apis/core2_a.php


getAddressFromLatLon()

Prototype

getAddressFromLatLon($lat='',$lon='')

Description

  • Reverse-geocodes a lat/long into a formatted postal address.
  • Use to label coordinates captured from a device GPS.

Parameters

  • $lat โ€” Latitude of location
  • $lon โ€” Longitude of location

Returns

  • String โ€” the formatted address for the coordinates.

Return example

echo getAddressFromLatLon(21.18, 72.83);
// 'Ring Road, Surat, Gujarat 395002, India'

Usage example

$distance = getAddressFromLatLon(21.22,72.2)
returns: Formatted address for a Geolocation

:material-file-code: Source: apis/core2_a.php


getDistance()

Prototype

getDistance($zip1, $zip2)

Description

  • Computes the distance between two postal ZIP/PIN codes, in kilometres.
  • Quick distance estimate when only pin codes are known.

Parameters

  • $zip1 โ€” ZIP Code 1
  • $zip2 โ€” ZIP Code 2

Returns

  • Number โ€” distance between the two ZIP points in kilometres.

Return example

echo getDistance('395003', '390001');
// e.g. 149  (km)

Usage example

$distance = getDistance( '395001', '122002' )
returns: distance between two zip codes in kms

:material-file-code: Source: apis/core2_a.php


getLnt()

Prototype

getLnt($zip)

Description

  • Returns the geolocation (lon/lat) for a postal ZIP/PIN code.
  • Use to place pin-coded records on a map.

Parameters

  • $zip โ€” Postal ZIP Code

Returns

  • Array โ€” longitude/latitude for the ZIP code.

Return example

$ll = getLnt('395003');
// ['lon'=>72.83, 'lat'=>21.18]

Usage example

$distance = getLnt( '395001' )
returns: Geolocation of the passed zip code

:material-file-code: Source: apis/core2_a.php