Search code examples
iosmathios6core-location

how to find nearest latitude and longitude form current place?


I have a dictionary with a collection of n number of latitude, longitude and address. I want to calculate which (latitude and langitude)point is closest from current user location(latitude ,langitude) in ios?

Thanks in advance


Solution

  • If you want to calculate based on the co-ordinates , here is simple scenario : Just take all of the "CLLocationCoordinate2D" from your dictionary .

    For ex:

    MKPointAnnotation *annotaion=[MKPointAnnotaion alloc]alloc];
    
    CLLocationCoordinate2D pointACoordinate = [annotation coordinate];
    //  If you have the co-ordinates in hand , then just make " CLLocation *loc2; " using Co-ordinates  
    
    loc2 = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];
    

    then put loop or whichever you want to calculate the less distance by comparing with all using this

    CLLocationDistance nearLocation=[ location distanceFromLocation:loc2]; // location --> your current Location
    

    If you want to achieve other than it : Then just follow saury answer, they are good to achieve perfectly.