Skip to content

Instantly share code, notes, and snippets.

@hugowetterberg
Created March 1, 2010 12:42
Show Gist options
  • Save hugowetterberg/318334 to your computer and use it in GitHub Desktop.
Save hugowetterberg/318334 to your computer and use it in GitHub Desktop.
Function to create a circle with a radius of X km, centered on a lat lon
<?php
/**
* Envelop a coordinate in a circle.
*
* @param string $lat
* Latitude.
* @param string $lon
* Longitude.
* @param float|int $radius
* Radius in km.
* @param int $steps
* Optional. The number of vertexes in the circle. Defaults to 8.
* @param bool $close
* Optional. Whether to close the circle. Defaults to FALSE.
* @return array
*/
function envelop_point($lat, $lon, $radius, $steps=8, $close=FALSE) {
$R = 6371; // The radius of earth
$delta_lon = rad2deg($radius/$R/cos(deg2rad($lat)));
$delta_lat = rad2deg($radius/$R);
$pts = array();
$step = pi() / ($steps / 2);
if ($close) {
$steps++;
}
for ($i = 0; $i < $steps; $i++) {
$rad = $step * $i;
$pts[] = array(
$lat + $delta_lat * cos($rad),
$lon + $delta_lon * sin($rad),
);
}
return $pts;
}
$pts = array();
foreach (envelop_point(55.6, 13.0, 3) as $c) {
$pts[] = "{$c[0]},{$c[1]}";
}
$coords = join('|', $pts);
shell_exec("open 'http://maps.google.com/maps/api/staticmap?path=color:0xff0000ff|weight:5|{$coords}&size=512x512&sensor=false'");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment