I am passing a center point (latitude, longitude) with a radius to this function to get the circumference points (latitude, longitude) so that I make a geometry from them and store it in MYSQL for me to do ST_Contains function later on.
This is getting me an oval shape instead of a circle when drawing them on Google Maps.
public static function convert($center, $radius, $numberOfSegments = 360)
{
$n = $numberOfSegments;
$flatCoordinates = [];
$coordinates = [];
for ($i = 0; $i < $n; $i++) {
$flatCoordinates = array_merge($flatCoordinates, static::offset($center, $radius, 2 * pi() * $i / $n));
}
$flatCoordinates[] = $flatCoordinates[0];
$flatCoordinates[] = $flatCoordinates[1];
for ($i = 0, $j = 0; $j < count($flatCoordinates); $j += 2) {
$coordinates[$i++] = array_slice($flatCoordinates, $j, 2);
}
return [
'type' => 'Polygon',
'coordinates' => [array_reverse($coordinates)]
];
}
public static function toRadians($angleInDegrees = null)
{
return $angleInDegrees * pi() / 180;
}
public static function toDegrees($angleInRadians = null)
{
return $angleInRadians * 180 / pi();
}
public static function offset($c1, $distance, $bearing)
{
$lat1 = static::toRadians($c1[1]);
$lon1 = static::toRadians($c1[0]);
$dByR = $distance / 6378137; // distance divided by 6378137 (radius of the earth) wgs84
$lat = asin(
sin($lat1) * cos($dByR) +
cos($lat1) * sin($dByR) * cos($bearing)
);
$lon = $lon1 + atan2(
sin($bearing) * sin($dByR) * cos($lat1),
cos($dByR) - sin($lat1) * sin($lat)
);
return [static::toDegrees($lon), static::toDegrees($lat)];
}
I do not really need it to be as accurate in terms of the earth curvature with taking into consideration WGS84, as the radius is quite small (25m-150m).