问题
I have a MapView with user location, and a bunch of Map annotations.
At first I want to show all the possible pins on the map. (succeeded in doing that)
Then I want to zoom in the map to show only the annotations that are within 50 KMs away from the userLocation annotation
How do i find these annotations?
回答1:
- (CLLocation*)closestLocationToLocation:(CLLocation*)currLocation
{
CLLocationDistance minDistance;
CLLocation *closestLocation = nil;
for (CLLocation *location in arrayOfLocations) {
CLLocationDistance distance = [location distanceFromLocation:currLocation];
if (distance <= minDistance
|| closestLocation == nil) {
minDistance = distance;
closestLocation = location;
}
}
//closestLocation is now the location from your array which is closest to the current location or nil if there are no locations in your array.
return closestLocation;
}
I think Its May be Very Helpful to You.Thanks!!
回答2:
You have to calculate the distance between pin and userlocation using :
CLLocationDistance dist = [loc1 distanceFromLocation:loc2];
where loc1 and loc2 are CLLocation objects. You have to filter the pins array with this distance parameter.
来源:https://stackoverflow.com/questions/24736046/how-to-find-map-annotations-that-fit-my-criteria