Search code examples
iosios9cllocationmanagerdistance

Distance for location always return result 0 - ios sdk


I had calculate 2 location distance as below and the result always be 0 meters. Can someone help?

double distance = [self.currentLocation distanceFromLocation:location1];
NSLog(@"Distance i meters: %.8f", distance);
if(distance <30)
{
    NSLog(@"Yeah, this place is inside my circle");
}
else
{
    NSLog(@"Oops!! its too far");
 }

Thanks


Solution

  • I get the correct result with the coding as below:

     CLLocation *location1 = [[CLLocation alloc] initWithLatitude:DEFAULT_LAT longitude:DEFAULT_LNG];
    
        CLLocationCoordinate2D annocoord = CLLocationCoordinate2DMake(location1.coordinate.latitude, location1.coordinate.longitude);
        CLLocationCoordinate2D usercoord = self.locationManager.location.coordinate;
    
       CLLocation *loc = [[CLLocation alloc] initWithLatitude:annocoord.latitude longitude:annocoord.longitude];
        CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.locationManager.location.coordinate.latitude longitude:self.locationManager.location.coordinate.longitude];
    
        NSLog(@"LOC  = %f, %f", loc.coordinate.latitude,  loc.coordinate.longitude);
        NSLog(@"LOC2 = %f, %f", loc2.coordinate.latitude, loc2.coordinate.longitude);
    
        CLLocationDistance dist = [loc distanceFromLocation:loc2];
    
        NSLog(@"DIST: %f", dist); // Wrong formatting may show wrong value!