我在使用getDistanceFrom和distanceFromLocation时遇到问题。 getDistanceFrom已过时,iOS 4.1已弃用,而distanceFromLocation在3.1.3中不可用,如何解决此问题。
还有其他人遇到这个问题吗?
目前,我的代码是:
CLLocationDistance kilometers;
if ([currentLocation respondsToSelector: @selector(distanceFromLocation:)])
{
kilometers = [currentLocation distanceFromLocation:previousLocation] / 1000;
}
else
{
//kilometers = [currentLocation getDistanceFrom:previousLocation] / 1000;
kilometers = [currentLocation performSelector: @selector(getDistanceFrom:) withObject: previousLocation] / 1000;
}
根据我使用的是哪个iOS,我会在行上获取“无效的操作数二进制”:
kilometers = [currentLocation distanceFromLocation:previousLocation] / 1000;
kilometers = [currentLocation performSelector: @selector(getDistanceFrom:) withObject: previousLocation] / 1000;
问候,
史提芬
最佳答案
CédricLuthi的blog post对此有一个很好的解决方案。
简而言之,您需要在main.m中输入以下内容并确保#import <objc/runtime.h>
:
Method getDistanceFrom = class_getInstanceMethod([CLLocation class], @selector(getDistanceFrom:));
class_addMethod([CLLocation class], @selector(distanceFromLocation:), method_getImplementation(getDistanceFrom), method_getTypeEncoding(getDistanceFrom));
然后,您可以在任何操作系统上使用
distanceFromLocation:
。