重複の可能性:
AndroidでGPSを介して距離を追跡する方法は?
私はGPSアプリケーションを設計し、それが私の位置をうまく伝えています。しかし今、私はより多くの機能を含めたいと思います。そこに半径を作成するにはどうすればよいですか?5または6kmの周辺エリアを持つために!そのエリアの場所と私の場所の間の距離をどのように言及できますか?
重複の可能性:
AndroidでGPSを介して距離を追跡する方法は?
私はGPSアプリケーションを設計し、それが私の位置をうまく伝えています。しかし今、私はより多くの機能を含めたいと思います。そこに半径を作成するにはどうすればよいですか?5または6kmの周辺エリアを持つために!そのエリアの場所と私の場所の間の距離をどのように言及できますか?
単に異なる座標があり、それらを使用して計算を行いたい場合は、すでに利用可能なAndroid関数を確認してください:http: //developer.android.com/reference/android/location/Location.html
Locationオブジェクトを作成し、set-functionsを使用して緯度/経度の座標を配置してから、
float distanceInMeters=location1.distanceTo(location2);
結果を得るために。
この質問がたくさんの質問になり始めているような気がします。質問のタイトル「GPSアプリケーションの距離」に向けて、この回答に取り組むことにしました。
私のアプリケーションでは、GoogleのAPIを使用する代わりに、次のようにしてGPS座標のリストからユーザーに距離を要求します。
私のJJMath
クラスでは:
距離の取得(Haversine Formula、マイル単位):
/**
* @param lat1
* Latitude which was given by the device's internal GPS or Network location provider of the users location
* @param lng1
* Longitude which was given by the device's internal GPS or Network location provider of the users location
* @param lat2
* Latitude of the object in which the user wants to know the distance they are from
* @param lng2
* Longitude of the object in which the user wants to know the distance they are from
* @return
* Distance from which the user is located from the specified target
*/
public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
return dist;
}
次に、その数値を次のように丸めます。
/** This gives me numeric value to the tenth (i.e. 6.1) */
public static double round(double unrounded) {
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(1, BigDecimal.ROUND_CEILING);
return rounded.doubleValue();
}
私はマップオーバーレイを使用していませんが、すばらしいチュートリアルや回答が来ると確信しています。