Find the nearest location from existing mapkit coordinates

I am developing an iphone application (goal C) for a customer with multiple stores. I have the coordinates (latitude, long) of all stores (20) in the array.

At the moment, Iā€™m thinking about going through an array of store coordinates and get the distance from the user's current location to the store location, and then add them to the array and sort them at the lowest distance.

Is this the right approach or a very resource hunger?

The closest SOF question was this one, but it is in python: retrieving nearby stores

Thank you in advance for your advice.

+4
source share
1 answer

To find the closest place among the selected data, you need to calculate the distance from the current location to each location that you have. And then just sort this data and you will get the closest location from the existing coordinate map.

Below is the method for determining 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]; } 
+6
source

Source: https://habr.com/ru/post/1384033/


All Articles