我使用这个函数来获得从一个CLLocation到另一个am的距离
这里的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.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公里
我做错了什么??
发布于 2013-02-03 06:24:49
您的代码行是这样写的:
coord.longitude = (CLLocationDegrees)[venue.lat doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lng doubleValue];显然,这应该是:
coord.longitude = (CLLocationDegrees)[venue.lng doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lat doubleValue];发布于 2013-02-03 06:24:55
在从venue设置coord的地方,纬度和经度是向后分配的。
这会给你带来意想不到的距离。
发布于 2013-02-03 06:30:30
查看您的输出:
LOC1 -73.986357 lat 40.702645 lng
ME 40.702082 lat -73.984775 lng你在我身上交换了lat和long
https://stackoverflow.com/questions/14667103
复制相似问题