私は Android アプリに取り組んでおり、店舗ロケーターの詳細とマップ ビューを追加したいと考えています。たくさん検索しましたが、意図的なものは何も得られません。緊張しており、あなたの助けが強く必要です。
問題は、2 つの座標間の距離を見つけたい、Haversine 式を 1 つ取得した、しかし、プログラムを正常に実行する方法がわからない、Android の初心者なので、段階的にコーディングします。つまり、xml コードも..、お願いします。
上で述べたように、Location クラスが最適です。
これが私が使用したコードです:
Location locationA = new Location("point A");
locationA.setLatitude(pointA.getLatitudeE6() / 1E6);
locationA.setLongitude(pointB.getLongitudeE6() / 1E6);
Location locationB = new Location("point B");
locationB.setLatitude(pointB.getLatitudeE6() / 1E6);
locationB.setLongitude(pointB.getLongitudeE6() / 1E6);
double distance = locationA.distanceTo(locationB);
この例では、pointAとpointBの両方が GeoPoint クラスのインスタンスです。
MotoDev スニペットから (GPS 座標間の距離):
Location originLocation = new Location("gps");
Location destinationLocation = new Location("gps");
originLocation.setLatitude(originLatitude);
originLocation.setLongitude(originLongitude);
destinationLocation.setLatitude(originLatitude);
destinationLocation.setLongitude(originLongitude);
float distance = originLocation.distanceTo(destinationLocation);
このコードを使用してください。2つの地理ポイント間の距離を取得します。
private String distance(double lati, double longi, double curlati1,
double curlongi1) {
double theta = longi - curlongi1;
double dist = Math.sin(deg2rad(lati)) * Math.sin(deg2rad(curlati1))
+ Math.cos(deg2rad(lati)) * Math.cos(deg2rad(curlati1))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
dist = dist * 1.609344;
result = Double.toString(dist);
System.out.println("dist_result :" + result);
return (result);
}
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :: This function converts decimal degrees to radians : */
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :: This function converts radians to decimal degrees : */
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
http://developer.android.com/reference/android/location/Location.html
distanceTo または distanceBetween を調べます。緯度と経度から Location オブジェクトを作成できます。
Location location = new Location("");
location.setLatitude(lat);
location.setLongitude(lon);