CLLocationDistance返回完全错误的值

时间:2013-02-02 22:16:30

标签: ios objective-c cllocation cllocationdistance

我正在使用此函数来获取从一个CLLocation到另一个CLLocation的距离

here venue.lat是NSNumber

GeoAddress *address = [[GeoAddress new] init];

CLLocationCoordinate2D coord;
coord.longitude = (CLLocationDegrees)[venue.lat doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lng doubleValue];

address.latlng = [[CLLocation alloc] initWithLatitude:coord.latitude longitude: coord.longitude];

if (curUserLocation.coordinate.latitude){
    address.distance = [address.latlng distanceFromLocation:curUserLocation];
    NSLog(@" DISTANCE %f", address.distance);
    NSLog(@" LOC1 %f lat %f lng", address.latlng.coordinate.latitude, address.latlng.coordinate.longitude);
    NSLog(@" ME %f lat %f lng", curUserLocation.coordinate.latitude, curUserLocation.coordinate.longitude);
}

这是Geoaddress:

//  GeoAddress.h
@interface GeoAddress : NSObject
{        
    CLLocation * latlng;
   CLLocationDistance  distance;
}
@property  CLLocationDistance  distance;
@property (nonatomic, retain)  CLLocation * latlng;

//  GeoAddress.m
#import "GeoAddress.h"

@implementation GeoAddress
@synthesize distance;
@synthesize  latlng;

这是日志:

DISTANCE 15106456.105786
LOC1 -73.986357 lat 40.702645 lng
ME 40.702082 lat -73.984775 lng

数字完全偏离实际距离~0.17 km

我做错了什么?

3 个答案:

答案 0 :(得分:5)

你有句话说:

coord.longitude = (CLLocationDegrees)[venue.lat doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lng doubleValue];

显然应该是:

coord.longitude = (CLLocationDegrees)[venue.lng doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lat doubleValue];

答案 1 :(得分:1)

如果您从coord设置venue,则会向后分配纬度和经度。

这会给你意想不到的距离。

答案 2 :(得分:1)

看看你的输出:

LOC1 -73.986357 lat 40.702645 lng
ME 40.702082 lat -73.984775 lng

你在ME中交换了lat和long