只是为了让您从一些代码开始,下面是一个遍历所有位置并返回最近位置的方法示例。
-(YourModel*)getClosestLocation
{
CLLocation *myLocation = currentLocation //This is the current location you should get from the GPS
NSArray *allLocations = myModelArray; //array with all locations you want to compare
YourModel *closestLocation = nil;
CLLocationDistance closestDistance = CGFLOAT_MAX;
for (YourModel* model in allLocations)
{
CLLocation *modelLocation = [[CLLocation alloc] initWithLatitude:model.latitude longitude:model.longitude];
CLLocationDistance distance = [myLocation distanceFromLocation:modelLocation];
if (distance < closestDistance) {
closestLocation = modelLocation;
closestDistance = distance;
}
}
return closestLocation;
}
请给予一些反馈,如果它有帮助。