如何计算两地之间的距离? iPhone 版 Google 地图有可用的服务吗?

发布于 2024-08-08 20:56:35 字数 76 浏览 3 评论 0原文

我想放置设施来显示用户选择的两个地点之间的距离。

是否有任何示例源代码/服务可用于执行此操作?

提前致谢。

I want to put facility to display distance between two places selected by user.

Is there any sample source code / service available to do so?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

小瓶盖 2024-08-15 20:56:35

CoreLocation Framework 提供了以下功能计算出两点之间的距离(以米为单位):

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

您可以使用纬度和经度初始化 CLLocation 对象:

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

The CoreLocation Framework provides the capability to work out the distance, in meters, between two points:

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

You can initialize a CLLocation object with latitude and longitude:

- (id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude
很快妥协 2024-08-15 20:56:35

如果您有两个 CLLocation 对象,您可以这样做:

[location1 getDistanceFrom:location2];

但是,这只是点对点。如果你想要特定路线上的距离,那就完全是另一回事了。

If you have two CLLocation objects, you can just do:

[location1 getDistanceFrom:location2];

However, that's just point-to-point. If you want the distance over a specific route, well, that's a whole other kettle of fish.

歌枕肩 2024-08-15 20:56:35

这是 CLLocation 框架的任务。使用已知的 2 个点坐标,您可以创建(如果还没有)2 个 CLLocation 对象,并使用

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

方法查找它们之间的距离。

This is a task for CLLocation framework. With known coordinates of 2 points you can create (if you don't have them already) 2 CLLocation objects and find a distance between them using

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

method.

将军与妓 2024-08-15 20:56:35

我不熟悉 iPhone 开发,但这里是使用半正弦公式计算两点之间距离的 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
}

I am not familiar with iPhone development, but here is the C# code for computing the distance between two points using the Haversine formula:

/// <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;

}

The MeasurementUnit is defined as:

/// <summary>
/// Measurement units
/// </summary>
public enum MeasurementUnit
{
    Miles,
    Kilometers
}
夕嗳→ 2024-08-15 20:56:35

这取决于您希望这两点之间有什么样的差异。在地图上我认为你不想要空中距离。然后你应该检查 http://iosboilerplate.com/

如果你仍然想使用空中距离,请使用核心位置。

It depends on which kind of difference you wanna have between both points. On the map I think you dont wanna the air distance. Then you should check http://iosboilerplate.com/

If you still wanna use the air distance, use Core Location.

绻影浮沉 2024-08-15 20:56:35
- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location

返回从接收器位置到指定位置的距离(以米为单位)。 (在 iOS 3.2 中已弃用。使用
距离位置:

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

Returns the distance (in meters) from the receiver’s location to the specified location. (Deprecated in iOS 3.2. Use
the distanceFromLocation:

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文