Skip to content

Instantly share code, notes, and snippets.

@dahse89
Created January 30, 2017 12:14
Show Gist options
  • Save dahse89/30da9006aab69a6413487ec8b1c173b9 to your computer and use it in GitHub Desktop.
Save dahse89/30da9006aab69a6413487ec8b1c173b9 to your computer and use it in GitHub Desktop.
Creating a geo point from another and a distance. Means adding distance to geo point
<?php
/**
* Class GeoOffsetByDistance
*/
class GeoOffsetByDistance
{
//Earth’s radius, sphere in meters
const R = 6378137;
/** @var float */
private $lat;
/** @var float */
private $lon;
/**
* GeoDistanceHelper constructor.
*
* @param $lat
* @param $lon
*/
public function __construct($lat, $lon)
{
$this->lat = $lat;
$this->lon = $lon;
}
/**
* @param $meters
* @return array
*/
public function offset($meters)
{
define('PI',pi());
//offsets in meters
// a² = b² + c² with b = c
// a² = b² + b² => a² = 2b² => b = a * sqrt(2)
$dn = $meters / sqrt(2);
$de = $meters / sqrt(2);
//Coordinate offsets in radians
$dLat = $dn/self::R;
$dLon = $de/(self::R*cos(deg2rad($this->getLatitude())));
//OffsetPosition, decimal degrees
$latO = $this->getLatitude() - rad2deg($dLat);
$lonO = $this->getLongitude() - rad2deg($dLon);
return ['lat' => $latO, 'lon' => $lonO];
}
/** @return float */
public function getLatitude()
{
return $this->lat;
}
/** @return float */
public function getLongitude()
{
return $this->lon;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment