Wednesday 20 June 2012

How to get distance between two location in iphone sdk

// .h
CLLocationManager *locationManager;
//
//ViewDidLoad
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest ;
[locationManager startUpdatingLocation];
//

// delegate start here
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D loc = [newLocation coordinate];
str1 = [NSString stringWithFormat: @"%.10f", loc.latitude];
NSLog(@"str1 %@",str1);
str2 = [NSString stringWithFormat: @"%.10f", loc.longitude];
NSLog(@"str2 %@",str2);

NSString *strsitename=[[arraySiteNameOnly objectAtIndex:0]objectForKey:@"Site_name"];

NSString *strlatitude = [[arrayLatitiude objectAtIndex:0]objectForKey:@"Latitude"];
NSString *finalLati = [strlatitude stringByReplacingOccurrencesOfString:@"N" withString:@"+"];
finalLati = [finalLati stringByReplacingOccurrencesOfString:@"S" withString:@"-"];
NSString *strllongitude = [[arrayLongitude objectAtIndex:arrayindex]objectForKey:@"Longitude"];
NSString *finalLongi = [strllongitude stringByReplacingOccurrencesOfString:@"E" withString:@"+"];
finalLongi = [finalLongi stringByReplacingOccurrencesOfString:@"W" withString:@"-"];

float distLatitude = 69.1 * ([finalLati floatValue] - [str1 floatValue]);
float disLongitude = 69.1 * ([finalLongi floatValue] - [str2 floatValue]) * cos([str1 floatValue]/57.3);
float distance= sqrt(distLatitude * distLatitude + disLongitude * disLongitude);

[locationManager stopUpdatingLocation];
}

2 comments:

  1. Thanks for the great post.

    -----------------------------------------------------------------
    iPhone 5 App Development And
    iPad Application Developer

    -------------------------------------------------------------------------------

    ReplyDelete