2点間の距離を求める

ここでは、GLanLngのdistanceFromを利用して2点間の距離を計測する方法を説明したいと思います。

サンプル

このサンプルでは、距離を求めている点をわかりやすくするためにGPolylineで線を引いています。 線の引き方に関しては「GPolylineで線を引く」をご覧下さい。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
    <script src="http://maps.google.com/maps?file=api&v=2&key=aaaa"
        type="text/javascript" charset="UTF-8"></script>
    <style type="text/css">
    v\:* {
      behavior:url(#default#VML);
    }
    </style>
  </head>
  <body>
    <div id="map" style="width:450px; height:400px"></div>
    <script type="text/javascript">
    //<![CDATA[

    if (GBrowserIsCompatible()) {
      var map = new GMap2(document.getElementById("map"));
      map.setCenter(new GLatLng(36.033333, 139.15), 11);

      var points = [];
      points[0] = new GLatLng(36.060, 139.15);
      points[1] = new GLatLng(36.020, 139.15);

      var polyline = new GPolyline(points);
      map.addOverlay(polyline);

      var distance = points[0].distanceFrom(points[1]);

      map.openInfoWindow(points[0], distance + " meters");
    }

    //]]>
    </script>
  </body>
</html>

distanceFromは2点間の距離をメートル単位で返します。 ただし、得られる値には0.3%ほどの誤差があるそうです。

上記サンプルでは、得られた距離(メートル)をopenInfoWindowで表示しています。

表示例

表示例です。



あきみち

Linuxネットワークプログラミングの本を書きました
Linuxネットワークプログラミング

Google Maps API
プログラミング



カスタム検索




はてなRSSに追加
Subscribe with livedoor Reader
Subscribe with Bloglines
Add to goo

外部サイト

プレコ王国
ディスカス魂
金魚タイムズ
YouTubeチャネル
Twitter
mixi(ほぼ未使用)


フィードメーター - Geekなぺーじ にほんブログ村 IT技術ブログへ
Copyright (C) Geekなページ.
All rights reserved. 無断転載や無断コピーなど、私的利用の範囲を逸脱した利用はおやめ下さい.