静的マップAPIはパスについて説明していますが、円については言及していません。これは可能ですか?ありがとう
4 に答える
できることは、エンコードされたポリラインアルゴリズムを使用して、ほぼ円形のパスを取得するのに十分なポイントを生成することです。確かにコーディングが必要です。円の中心と半径を取得し、それを一連の緯度/経度に変換してから、アルゴリズムを使用してエンコードする必要があります。
別の方法として、透明なgif画像をマーカーとして使用し、それをマップに配置できる場合があります。
PolyLine
Google Static Mapsは円自体の描画をサポートしていないため、詳細な描画で円を表すことができます。
これは、半径0.5kmと5kmの2つの円を含むリクエストの例です。適切なエンコーディングポリラインを生成するには、アルゴリズムが必要であることに注意してください。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'];
}
jomacincのチュートリアルのおかげで、お楽しみください:)
円周に沿った座標を取得できる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) + '|';
}
可能ですが、円のように見えるように、辺がたくさんある形を描く必要があります。
これが私の例です:
<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を使用しました