Reputation: 409
I am trying to get a distance from my longitude and latitude i have stored into core data, to the current location. I have this code, but i cannot figure out how to integrate the code for using the coordinates i have stored in core data. How do i use my coordinates stored in core data to get distance from user location? Thank you for any help!
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];
I get my core data coordinates like this:
_buyLong.text = [[_itemDetailArray valueForKey:@"longitude"] description];
_buyLatitude.text = [[_itemDetailArray valueForKey:@"latitude"] description];
Upvotes: 0
Views: 287
Reputation: 409
Figured it out so hopefully someone can benefit:
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;
Upvotes: 1