Stp*_*tpn 0 objective-c cllocation ios cllocationdistance
我正在使用此函数来获取从一个CLLocation到另一个CLLocation的距离
这里的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);
}
Run Code Online (Sandbox Code Playgroud)
这是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;
Run Code Online (Sandbox Code Playgroud)
这是日志:
DISTANCE 15106456.105786
LOC1 -73.986357 lat 40.702645 lng
ME 40.702082 lat -73.984775 lng
Run Code Online (Sandbox Code Playgroud)
数字完全偏离实际距离~0.17 km
我究竟做错了什么??
你有线说:
coord.longitude = (CLLocationDegrees)[venue.lat doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lng doubleValue];
Run Code Online (Sandbox Code Playgroud)
显然应该是:
coord.longitude = (CLLocationDegrees)[venue.lng doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lat doubleValue];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1793 次 |
| 最近记录: |