在Google静态地图上画圆是可能的吗?

9
静态地图API中讨论了路径,但没有提到圆形。这是否可行? 谢谢
4个回答

6

由于Google Static Maps不支持绘制圆形,因此可以通过绘制详细的PolyLine来表示一个圆。

需要算法生成适当的编码多段线。我在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'];
    }

享受吧!


可能应该给我从这里拿走的代码署上出处。https://dev59.com/6Ww05IYBdhLWcg3wTwKY#10715947 - Jomac

5
你可以使用编码折线算法来生成足够的点以获得大致圆形路径。这需要编程:您需要获取圆心和半径,将其转换为一系列经纬度,然后使用该算法进行编码。
另外,您也可以使用透明的gif图像作为标记放置在地图上。

2
请注意标记有大小限制。对我来说,100像素乘以100像素不起作用,但75像素乘以75像素可以。 - kingjeffrey
问题在于,根据圆的长度,您可能需要大量的点才能使其真正看起来像一个圆,因此您可能会短缺URL字符(目前限制为1024个字符)。 - Renato Gama
2048 characters, sorry - Renato Gama

4
您可以使用computeOffset方法来近似圆形,该方法允许您获取沿周长的坐标。在此示例中,我使用了8度的递增,这将给出45对坐标。对于圆的尺寸,我使用的圆在静态地图中呈现出漂亮的圆形。如果您使用的是大圆,则只需将增量更改为较小的值,例如j = j + 6。变量pathText添加到所需的静态地图的其余web地址中。
var pathText = '&path=';
var circumLatLng;

for (var j = 0; j < 361; j = j + 8) {

circumLatLng = google.maps.geometry.spherical.computeOffset(circle.getCenter(), circle.getRadius(), j);

pathText += circumLatLng.lat().toFixed(6) + ',' + circumLatLng.lng().toFixed(6) + '|';
}

2

这是可能的,但您需要绘制一个有很多边的形状来看起来像一个圆形。

以下是我的示例:

<img src="http://maps.google.com/maps/api/staticmap?size=600x500&path=fillcolor:0x0000FF|weight:3|color:0xFF0000|enc:ue{cI|rrH`@qs@hBis@nDyr@rF_r@tH_q@xJyo@zLgn@vNol@rPsj@lRmh@`Taf@tUqc@dWy`@nX{]vY{ZxZuWx[kTr\_Qh]oMz]}If^iFl^uBn^?n^tBd^hFz]|Ih]nMr\~Px[jTxZtWvYzZnXz]dWx`@tUpc@`T`f@lRlh@rPrj@xNnl@xLfn@xJxo@vH~p@rF~q@lDxr@hBhs@b@ps@c@ps@iBfs@mDxr@sF~q@wH`q@yJvo@yLfn@yNpl@sPpj@mRlh@aTbf@uUnc@eWx`@oX|]wYzZyZtWy[jTs\~Pi]lM{]|Ie^jFo^rBo^?m^sBg^kF{]}Ii]mMs\_Qy[kTyZuWwY{ZoX}]eWy`@uUoc@aTcf@mRmh@sPqj@wNql@{Lgn@yJwo@uHaq@sF_r@oDyr@iBgs@a@qs@&sensor=true" border="0"/>

或者尝试使用此链接查看

我使用freemaptools.com生成了这个。


有没有办法使它动态化?如果我有许多不同的坐标要使用呢? - Jonas Borggren
@JonasB 可以实现。寻找折线生成器。如果有用的话,我已经为此编写了一个 Angular 指令。 - sidonaldson
值得注意的是,该网站使用了PHP折线编码器(来自客户端源代码:xmlHttp.open("POST","ajax/csv-polyline-encoder.php",true);)。 - Drew Chapin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接