In an app I need to calculate the distance between two point. I'm doing it like this.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
NSLog(@"updating");
//Getting my concerts
arrConcerten = [matches mutableCopy];
for (Event *concert in arrConcerten) {
CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:[concert.eve_latitude floatValue] longitude:[concert.eve_longitude floatValue]];
CLLocationDistance meters = [restaurnatLoc distanceFromLocation:newLocation];
NSLog(@"meters are %f",meters);
if(meters > 5000){
[locationManager2 stopUpdatingLocation];
NSString *message2 = [NSString stringWithFormat:@"Long = %f and lat= %f and Concert Lat = %f and long = %f",newLocation.coordinate.longitude,newLocation.coordinate.latitude,[concert.eve_latitude floatValue],[concert.eve_longitude floatValue]];
NSLog(@"%@",message2);
}else{
NSLog(@"too far away");
}
}
}
This is what my log says.
2013-10-17 15:57:23.518 Domino[7468:907] meters are 7973.753294
2013-10-17 15:57:23.520 Domino[7468:907] Long = 5.621456 and lat= 51.097340 and Concert Lat = 51.038139 and long = 5.557330
2013-10-17 15:57:23.896 Domino[7468:907] meters are 27850.874245
2013-10-17 15:57:23.899 Domino[7468:907] Long = 5.621456 and lat= 51.097340 and Concert Lat = 50.860332 and long = 5.493722
Like you can see, the locations are near each other. but it still says that we are too far away. Can somebody help me ?
Why are you using this:
if it s over 5000 meter: do something, else too far away? I don't understand the logic. If its too far away it's over 5000 meter. So I think:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
NSLog(@"updating");
//Getting my concerts
arrConcerten = [matches mutableCopy];
for (Event *concert in arrConcerten) {
CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:[concert.eve_latitude floatValue] longitude:[concert.eve_longitude floatValue]];
CLLocationDistance meters = [restaurnatLoc distanceFromLocation:newLocation];
NSLog(@"meters are %f",meters);
if(meters <= 5000){
[locationManager2 stopUpdatingLocation];
NSString *message2 = [NSString stringWithFormat:@"Long = %f and lat= %f and Concert Lat = %f and long = %f",newLocation.coordinate.longitude,newLocation.coordinate.latitude,[concert.eve_latitude floatValue],[concert.eve_longitude floatValue]];
NSLog(@"%@",message2);
}else{
NSLog(@"too far away");
}
}
}
or of corse:
if(meters > 5000){
NSLog(@"it's far enough");
}else{
NSLog(@"too close");
}
And there is a really important thing:
This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations. from apple documentation
distanceFromLocation measure the distance "in the air", not a turn by turn measure.
Here is the Solution-
-(NSString *)getDistance:(NSDictionary *)dicVal{
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:[[dicVal valueForKey:@"latitude"] doubleValue] longitude:[[dicVal valueForKey:@"longitude"] doubleValue]];
CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:appDelegate.userLocation.latitude longitude:appDelegate.userLocation.longitude];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
NSString *dist=[NSString stringWithFormat:@"%4.0f %@", distance/1000,NSLocalizedString(@"KM", nil)];
return dist;
}
Yes its just issue of allocation you have to allocate every time cllocation
manager like as below:
LocationManager = [[[CLLocationManager alloc] init] autorelease];
LocationManager.delegate = self;
LocationManager.desiredAccuracy = kCLLocationAccuracyBest;
LocationManager.distanceFilter = kCLDistanceFilterNone;
[LocationManager startUpdatingLocation];
CLLocation *location = [LocationManager location];
CLLocationCoordinate2D coordinate1 = [location coordinate];
CLLocation *currentLoc = [[CLLocation alloc] initWithLatitude:coordinate1.latitude longitude:coordinate1.longitude];
CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:l@"Your latitude not current" longitude:@"Your longitude not current'];
CLLocationDistance meters = [restaurnatLoc distanceFromLocation:currentLoc];
if (meters > 1000)
{
Distancelbl.text=[NSString stringWithFormat: @"You are Away %.2f Km",meters];
}
else{
Distancelbl.text=[NSString stringWithFormat: @"You are Away %.2f Mtr",meters];
}
`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.