Ios CLLocationDistance返回完全错误的值
我使用这个函数来获得从一个位置到另一个位置的距离 这里是VICENUE.lat是NSNumberIos CLLocationDistance返回完全错误的值,ios,objective-c,cllocation,cllocationdistance,Ios,Objective C,Cllocation,Cllocationdistance,我使用这个函数来获得从一个位置到另一个位置的距离 这里是VICENUE.lat是NSNumber GeoAddress *address = [[GeoAddress new] init]; CLLocationCoordinate2D coord; coord.longitude = (CLLocationDegrees)[venue.lat doubleValue]; coord.latitude = (CLLocationDegrees)[venue.lng doubleValue];
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.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公里
我做错了什么???你的台词上写着:
coord.longitude = (CLLocationDegrees)[venue.lat doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lng doubleValue];
显然,这应该是:
coord.longitude = (CLLocationDegrees)[venue.lng doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lat doubleValue];
在您从
场地设置坐标的地方,纬度和经度是向后分配的
这会给您带来意想不到的距离。查看您的输出:
LOC1 -73.986357 lat 40.702645 lng
ME 40.702082 lat -73.984775 lng
你在我身上交换了lat和long在模拟器中,我也得到了一个错误的距离,我猜返回了一个默认位置,只是因为它不是真正的设备。我认为LOC1的纬度和经度是向后的。噢,哇。。谢谢您。真不敢相信我只是试图通过实际的公式来转换它们。谢谢,这让我明白我做错了什么:P