是否可以在Google静态地图上绘制圆圈?

Ane*_*apu 9 google-maps google-maps-static-api

静态地图API讨论路径,但没有提及圈子.这可能吗?谢谢

Ton*_*ler 5

您可以做的是使用编码折线算法生成足够的点以获得大致圆形的路径.肯定涉及编码:您需要获得圆的中心和半径,将其转换为一系列纬度/长度,然后使用算法进行编码.

作为替代方案,您可以使用透明的gif图像作为标记并将其放在地图中.

  • 请注意,标记具有大小限制.100px乘100px对我不起作用,但75px乘75px. (2认同)

Men*_*ris 5

您可以通过详细绘图来表示圆形PolyLine,因为Google静态地图不支持自己绘制圆形.

这是一个请求示例,其中包含2个半径为0.5和5千米的.请记住,您需要一个算法才能生成正确的编码折线.我使用这个实现,因为我在PHP编码,但你可以根据你想要的语言自己找到或开发类似的东西.

这是我用来生成请求的PHP代码:

<?php
/* set some options */
$mapLat = filter_input(INPUT_POST, 'lat1'); // latitude for map's and circle's center
$mapLng = filter_input(INPUT_POST, 'lon1'); // longitude for map's and circle's center
$mapRadius1 = 0.5; // the radius of the first circle (in Kilometres)
$mapRadius2 = 5; // the radius of the second circle (in Kilometres)
$mapFill_first = '330000'; // fill colour of the first circle
$mapFill_second = 'FF99FF'; // fill colour of the second circle
$map1Border1 = '91A93A'; // border colour of the first circle
$map1Border2 = '0000CC'; // border colour of the second circle
$mapWidth = 450; // map image width (max 640px)
$mapHeight = 450; // map image height (max 640px)
$zoom = 11;
$scale = 2;
/** create our encoded polyline string for the first circle*/
$EncString1 = GMapCircle($mapLat, $mapLng, $mapRadius1);
/** create our encoded polyline string for the second circle*/
$EncString2 = GMapCircle($mapLat, $mapLng, $mapRadius2);
/** put together the static map URL */
$MapAPI = 'http://maps.google.com.au/maps/api/staticmap?';
$MapURL = $MapAPI . 'center=' . $mapLat . ',' . $mapLng . '&zoom=' . $zoom . '&size=' .
    $mapWidth . 'x' . $mapHeight . '&scale=' . $scale . '&markers=color:red%7Clabel:S%7C'.$mapLat.','.$mapLng .
    '&maptype=roadmap&path=fillcolor:0x' . $mapFill_first .
    '33%7Ccolor:0x' . $map1Border1 . '00%7Cenc:' . $EncString1 . '&path=fillcolor:0x' .
    $mapFill_second . '33%7Ccolor:0x' . $map1Border2 . '00%7Cenc:' . $EncString2;

/* output an image tag with our map as the source */
//echo '<img src="' . $MapURL . '" />';
echo json_encode($MapURL);

function GMapCircle($Lat, $Lng, $Rad, $Detail = 8)
{
    $R = 6371;
    $pi = pi();
    $Lat = ($Lat * $pi) / 180;
    $Lng = ($Lng * $pi) / 180;
    $d = $Rad / $R;
    $points = array();
    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);
    }

    require_once('PolylineEncoder.php');
    $PolyEnc = new PolylineEncoder($points);
    $EncString = $PolyEnc->dpEncode();

    return $EncString['Points'];
}
Run Code Online (Sandbox Code Playgroud)

感谢jomacinc的教程,享受:)