カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

2点間の距離を取得 – Location.distanceBetween

この記事は最終更新日から1年以上経過しています。

LocationクラスのdistanceBetweenメソッドを使って、2点間の距離を取得することができます。

書式

Location.distanceBetween(
        double startLatitude,
        double startLongitude,
        double endLatitude,
        double endLongitude,
        float[] results);
引数
startLatitude 開始地点の緯度
startLongitude 開始地点の経度
endLatitude 終了地点の緯度
endLongitude 終了地点の経度
results 結果を格納する変数

結果
results[0] 2点間の距離(単位:m)
results[1] 終了地点までの方位角
results[2] 終了地点からの方位角

例)

import android.location.Location;

float[] results = new float[] {};
String distance = "";

try {
    Location.distanceBetween(
            startLatitude,
            startLongitude,
            endLatitude,
            endLongitude,
            results);
    
    if(results != null && results.length > 0) {
        distance = String.valueOf((double)results[0]) + "m";
    }

} catch (IllegalArgumentException ex) {
    
}

参考URL:
http://developer.android.com/intl/ja/reference/android/location/Location.html#distanceBetween(double, double, double, double, float[])

この記事がお役に立ちましたらシェアお願いします
9,145 views

コメントは受け付けていません。