2012-11-21 60 views
0

我試圖從我的經度和緯度獲得距離,這些經度和緯度是存儲在覈心數據中的當前位置。我有這個代碼,但我無法弄清楚如何集成代碼以使用我存儲在覈心數據中的座標。我如何使用存儲在覈心數據中的座標來獲取距用戶位置的距離?感謝您的任何幫助!需要從存儲的座標到當前位置的距離

CLLocation *to = [[CLLocation alloc] 
        initWithLatitude:mapView.userLocation.coordinate.latitude 
        longitude:mapView.userLocation.coordinate.longitude]; 

CLLocation *from = [[CLLocation alloc] 
        initWithLatitude:mapView.userLocation.coordinate.latitude 
        longitude:mapView.userLocation.coordinate.longitude]; 

CLLocationDistance distance = [to distanceFromLocation:from]; 

_distance.text = [[NSString alloc]initWithFormat:@"%g",distance]; 

我得到我的核心數據座標是這樣的:

_buyLong.text = [[_itemDetailArray valueForKey:@"longitude"] description]; 
_buyLatitude.text = [[_itemDetailArray valueForKey:@"latitude"] description]; 

回答

1

想通了,所以希望有人能受益於:

NSString *savedLatitude = [[_itemDetailArray valueForKey:@"latitude"] description]; 
    double latDouble = [savedLatitude doubleValue]; 

NSString *savedLongitude = [[_itemDetailArray valueForKey:@"longitude"] description]; 
    double longDouble = [savedLongitude doubleValue]; 

NSLog(@"Value Lat: %f", latDouble); 
NSLog(@"Value Long: %f", longDouble); 

// Item Location from Array 
itemCoordinate.latitude = latDouble; 
itemCoordinate.longitude = longDouble; 

// User Location from Device 
[currentLocation stopUpdatingLocation]; 
theCoordinate.latitude = newLocation.coordinate.latitude; 
theCoordinate.longitude = newLocation.coordinate.longitude; 

CLLocation *loc = [[CLLocation alloc] initWithLatitude:theCoordinate.latitude longitude:theCoordinate.longitude]; 
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:itemCoordinate.latitude longitude:itemCoordinate.longitude]; 

CLLocationDistance dist = [loc distanceFromLocation:loc2] *0.0006213; // Convert Meters to Miles 
NSString *distance = [NSString stringWithFormat:@"%.1f",dist]; 

NSLog(@"DIST in Miles: %f", dist); // Wrong formatting may show wrong value!} 

_distance.text = distance; 
相關問題