Find nearest location from existing coordinates mapkit

不问归期 提交于 2019-12-03 16:28:32

To find nearest location among fetched data, you need to calculate distance from current location to each location you are having. And then, just sort those data and you will get the nearest location from existing coordinate mapkit.

Following is a method to find out the distance...

-(float)kilometresBetweenPlace1:(CLLocationCoordinate2D) currentLocation andPlace2:(CLLocationCoordinate2D) place2 
{
    CLLocation *userLoc = [[CLLocation alloc] initWithLatitude:currentLocation.latitude longitude:currentLocation.longitude];
    CLLocation *poiLoc = [[CLLocation alloc] initWithLatitude:place2.latitude longitude:place2.longitude];  
    CLLocationDistance dist = [userLoc getDistanceFrom:poiLoc]/(1000*distance);
    NSString *strDistance = [NSString stringWithFormat:@"%.2f", dist];
    NSLog(@"%@",strDistance);
    return [strDistance floatValue];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!