使用CLLocation计算两个坐标之间的距离 [英] Calculating Distance between two coordinates using CLLocation

查看:683
本文介绍了使用CLLocation计算两个坐标之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 CLLocationDistance 来获得两点之间的距离,但是在传递我当前位置时出现错误。

I'm using CLLocationDistance to get the distance between two points, but I'm getting an error when passing my current location in it.

CLLocation *current = [[CLLocation alloc] initWithLatitude:startLocation.coordinate.latitude longitude:startLocation.coordinate.longitude];
CLLocation *itemLoc = [[CLLocation alloc] initWithLatitude:[[[getName objectAtIndex:indexPath.row] objectForKey:@"lat"] doubleValue] longitude:[[[getName objectAtIndex:indexPath.row] objectForKey:@"lon"] doubleValue]];

//Here the current location gives me an error "Initializing cllocation with an expression incompatible format"
CLLocationDistance *itemDist = [itemLoc distanceFromLocation:current];
NSLog(@"Distance: %@", itemDist);


推荐答案

p>

The error you're getting is actually:


使用不兼容类型CLLocationDistance(也称为'double')初始化'CLLocationDistance *'(aka'double *')

Initializing 'CLLocationDistance *' (aka 'double *') with an expression of incompatible type 'CLLocationDistance' (aka 'double')

它的意思是你正在初始化 itemDist CLLocationDistance (通知无星号)

What it's saying is you're initializing itemDist (which you've declared as a CLLocationDistance *) to something that is returning a CLLocationDistance (notice no asterisk).

CLLocationDistance 不是对象。

它只是一个原始类型(特别是 double - 请参阅核心位置数据类型参考)。



因此, code>作为指针添加到 CLLocationDistance ,只需将其声明为 CLLocationDistance (no asterisk):


So instead of declaring itemDist as a pointer to a CLLocationDistance, just declare it as a CLLocationDistance (no asterisk):

CLLocationDistance itemDist = [itemLoc distanceFromLocation:current];

您还需要更新 NSLog 以期望 double 而不是对象,否则会在运行时崩溃:

You'll also need to update the NSLog to expect a double instead of an object otherwise it will crash at run-time:

NSLog(@"Distance: %f", itemDist);

这篇关于使用CLLocation计算两个坐标之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆