2013-02-02 23 views
0

我使用這個功能去另一個CLLocationDistance回報完全錯誤的價值觀

這裏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從一個CLLocation的距離:

// 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公里

我在做什麼錯?

+0

在模擬器我得到一個錯誤的距離太遠,我想有一個默認的位置返回,只是因爲它不是一個真正的設備。 –

+0

我認爲LOC1的經緯度是向後的。 – Anna

回答

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]; 
+0

哦哇..謝謝。我不敢相信我只是試圖通過實際的公式來轉換它們。 – Stpn

+0

謝謝,這只是告訴我我做錯了什麼:P – mattdeboard

1

你從哪兒venue設置coord,緯度和經度被向後分配。

這給你意想不到的距離。

1

看看你的輸出:

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

你在我交換lat和長