Drawing a circle Google Static Maps

后端 未结 7 687
刺人心
刺人心 2020-12-07 23:39

I have a Google Maps Circle drawn on v3 api. When the user has plotted there circle (or polygon if they choose), they can save the data to the server. If the user has picked

7条回答
  •  情歌与酒
    2020-12-07 23:57

    A bit late in the game, but nothing I found solved my issue (serverside php only, no javascript). I ended up getting there in the end and have detailed my method here: http://jomacinc.com/map-radius/ and the short version is below.

    This PHP function will return an encoded polyline string of lat/lng points in a circle around the specified point, and at the specified radius. The function requires Gabriel Svennerberg’s PHP polyline encoding class available here (http://www.svennerberg.com/examples/polylines/PolylineEncoder.php.txt).

    function GMapCircle($Lat,$Lng,$Rad,$Detail=8){
     $R    = 6371;
    
     $pi   = pi();
    
     $Lat  = ($Lat * $pi) / 180;
     $Lng  = ($Lng * $pi) / 180;
     $d    = $Rad / $R;
    
     $points = array();
     $i = 0;
    
     for($i = 0; $i <= 360; $i+=$Detail):
       $brng = $i * $pi / 180;
    
       $pLat = asin(sin($Lat)*cos($d) + cos($Lat)*sin($d)*cos($brng));
       $pLng = (($Lng + atan2(sin($brng)*sin($d)*cos($Lat), cos($d)-sin($Lat)*sin($pLat))) * 180) / $pi;
       $pLat = ($pLat * 180) /$pi;
    
       $points[] = array($pLat,$pLng);
     endfor;
    
     require_once('PolylineEncoder.php');
     $PolyEnc   = new PolylineEncoder($points);
     $EncString = $PolyEnc->dpEncode();
    
     return $EncString['Points'];
    }
    

    You can now the use the above function to create a static map.

    /* set some options */
    $MapLat    = '-42.88188'; // latitude for map and circle center
    $MapLng    = '147.32427'; // longitude as above
    $MapRadius = 100;         // the radius of our circle (in Kilometres)
    $MapFill   = 'E85F0E';    // fill colour of our circle
    $MapBorder = '91A93A';    // border colour of our circle
    $MapWidth  = 640;         // map image width (max 640px)
    $MapHeight = 480;         // map image height (max 640px)
    
    /* create our encoded polyline string */
    $EncString = GMapCircle($MapLat,$MapLng, $MapRadius);
    
    /* put together the static map URL */
    $MapAPI = 'http://maps.google.com.au/maps/api/staticmap?';
    $MapURL = $MapAPI.'center='.$MapLat.','.$MapLng.'&size='.$MapWidth.'x'.$MapHeight.'&maptype=roadmap&path=fillcolor:0x'.$MapFill.'33%7Ccolor:0x'.$MapBorder.'00%7Cenc:'.$EncString.'&sensor=false';
    
    /* output an image tag with our map as the source */
    echo ''
    

提交回复
热议问题