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];
4

1 回答 1

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;
于 2012-11-22T16:55:48.017 回答