2つの場所間の距離を計算する方法は? iPhone向けGoogleマップで利用できるサービスはありますか?

StackOverflow https://stackoverflow.com/questions/1643138

  •  10-07-2019
  •  | 
  •  

質問

ユーザーが選択した2つの場所の間の距離を表示する施設を設置したい。

使用可能なサンプルソースコード/サービスはありますか?

事前に感謝します。

役に立ちましたか?

解決

CoreLocationフレームワーク 2つのポイント間の距離をメートル単位で計算する機能を提供します。

- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location

緯度と経度で CLLocation オブジェクトを初期化できます:

- (id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude

他のヒント

2つのCLLocationオブジェクトがある場合は、次の操作を実行できます。

[location1 getDistanceFrom:location2];

ただし、これは単なるポイントツーポイントです。特定のルートでの距離が必要な場合は、他の魚のケトルです。

これはCLLocationフレームワークのタスクです。 2ポイントの既知の座標を使用して、2つのCLLocationオブジェクトを作成し(まだ持っていない場合)、

を使用してそれらの間の距離を見つけることができます。
- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location

メソッド。

iPhoneの開発には慣れていませんが、Haversineの式を使用して2点間の距離を計算するC#コードを次に示します。

/// <summary>
/// Computes the distance beween two points
/// </summary>
/// <param name="P1_Latitude">Latitude of first point (in radians).</param>
/// <param name="P1_Longitude">Longitude of first point(in radians).</param>
/// <param name="P2_Latitude">Latitude of second point (in radians).</param>
/// <param name="P2_Longitude">Longitude of second point (in radians).</param>
protected double ComputeDistance(double P1_Longitude, double P1_Latitude, double P2_Longitude, double P2_Latitude, MeasurementUnit unit)
{            
    double dLon = P1_Longitude - P2_Longitude;
    double dLat = P1_Latitude - P2_Latitude;

    double a = Math.Pow(Math.Sin(dLat / 2.0), 2) +
            Math.Cos(P1_Latitude) *
            Math.Cos(P2_Latitude) *
            Math.Pow(Math.Sin(dLon / 2.0), 2.0);

    double c = 2 * Math.Asin(Math.Min(1.0, Math.Sqrt(a)));
    double d = (unit == MeasurementUnit.Miles ? 3956 : 6367) * c;
    return d;

}

MeasurementUnitは次のように定義されています:

/// <summary>
/// Measurement units
/// </summary>
public enum MeasurementUnit
{
    Miles,
    Kilometers
}

それは、両方のポイントの間にどのような違いを持ちたいかによって異なります。地図上で、私はあなたが空中距離を望んでいないと思う。次に、 http://iosboilerplate.com/

を確認する必要があります。

空中距離を使用したい場合は、コアロケーションを使用します。

- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location

受信者の位置から指定された位置までの距離(メートル単位)を返します。 (iOS 3.2で非推奨。使用 distanceFromLocation:

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top