且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用CLLocation计算两个坐标之间的距离

更新时间:2022-04-18 22:00:14

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