How to find map annotations that fit my criteria

你说的曾经没有我的故事 提交于 2019-12-25 17:19:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!