首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CLLocationDistance返回完全错误的值

CLLocationDistance返回完全错误的值
EN

Stack Overflow用户
提问于 2013-02-03 06:16:30
回答 3查看 1.8K关注 0票数 0

我使用这个函数来获得从一个CLLocation到另一个am的距离

这里的venue.lat是NSNumber

代码语言:javascript
复制
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);
}

这是地理地址:

代码语言:javascript
复制
//  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;

这是日志:

代码语言:javascript
复制
DISTANCE 15106456.105786
LOC1 -73.986357 lat 40.702645 lng
ME 40.702082 lat -73.984775 lng

这些数字完全不一致,实际距离约0.17公里

我做错了什么??

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-03 06:24:49

您的代码行是这样写的:

代码语言:javascript
复制
coord.longitude = (CLLocationDegrees)[venue.lat doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lng doubleValue];

显然,这应该是:

代码语言:javascript
复制
coord.longitude = (CLLocationDegrees)[venue.lng doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lat doubleValue];
票数 5
EN

Stack Overflow用户

发布于 2013-02-03 06:24:55

在从venue设置coord的地方,纬度和经度是向后分配的。

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

票数 1
EN

Stack Overflow用户

发布于 2013-02-03 06:30:30

查看您的输出:

代码语言:javascript
复制
LOC1 -73.986357 lat 40.702645 lng
ME 40.702082 lat -73.984775 lng

你在我身上交换了lat和long

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14667103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档