blog-banner

KML - Google Map Circle Generator Script in PHP

  • GMap
  • PHP

KML Google Maps

 
Here's a simple example to generate a KML circle by the meter at a specific center of point (lat, long) on Google maps by using a simple PHP code.
 
 
  1. $lat = '37.029869';
  2. $long = '-76.345222';
  3. $meter = 1000;
  4.  
  5. // Get circle coordinates
  6. $coordinatesList = getCirclecoordinates($lat, $long, $meter);
  7.  
  8. // Output
  9. $kml .= "    \n";
  10. $kml .= "     \n";
  11. $kml .= "      \n";
  12. $kml .= "       ".$coordinatesList."\n";
  13. $kml .= "      \n";
  14. $kml .= "     \n";
  15. $kml .= "    \n";
  16.  
  17. function getCirclecoordinates($lat, $long, $meter) {
  18.   // convert coordinates to radians
  19.   $lat1 = deg2rad($lat);
  20.   $long1 = deg2rad($long);
  21.   $d_rad = $meter/6378137;
  22.  
  23.   $coordinatesList = "";
  24.   // loop through the array and write path linestrings
  25.   for($i=0; $i<=360; $i+=3) {
  26.     $radial = deg2rad($i);
  27.     $lat_rad = asin(sin($lat1)*cos($d_rad) + cos($lat1)*sin($d_rad)*cos($radial));
  28.     $dlon_rad = atan2(sin($radial)*sin($d_rad)*cos($lat1), cos($d_rad)-sin($lat1)*sin($lat_rad));
  29.     $lon_rad = fmod(($long1+$dlon_rad + M_PI), 2*M_PI) - M_PI;
  30.     $coordinatesList .= rad2deg($lon_rad).",".rad2deg($lat_rad).",0 ";
  31.   }
  32.   return $coordinatesList;
  33. }
 
Hope this blog helps you to generate a KML file in Google Maps through PHP code.