How to get the distance of multiple location in array from one location and Sort that array by nearest distance in iOS?

B.Saravana Kumar

I am working on a project in which I have to show the distance of multiple locations from one location. locations are based on latitude and longitude.

I am using the following code to get the distance between two locations is shows nearly same distance

CLLocation *locationA = [[CLLocation alloc] initWithLatitude:28.6379 longitude: 77.2432];CLLocation *locationB = [[CLLocation alloc] initWithLatitude:28.6562 longitude:77.2410];CLLocationDistance distance = [locationA distanceFromLocation:locationB];NSLog(@"Distance is %f",distance);float i  = distance/1000;NSLog(@"distance between two places is %f KM", i);

but now i am struct to get the distance of multiple locations from my location: locationA.

for example I take NSArray for latitude and longitude as

NSArray * latitudeArray = [[NSArray alloc]initWithObjects:@"28.6129",@"28.6020",@"28.5244", nil];NSArray * longitudeArray = [[NSArray alloc]initWithObjects:@"77.2295",@"77.2478",@"77.1855", nil];

Please help me to resolve it..

Take locationA as one location..

Please help me to sort the Array by nearest Distance..

PlusInfosys

First of all, don't create two array for latitude and longitude, It should be one array of CLLocations.

  NSMutableArray locationsArray = [[NSMutableArray alloc] init];
    //This is just for example, You should add locations to this array according to format of data you have available.

    [locationsArray addObject:[[CLLocation alloc] initWithLatitude:28.6379 longitude:77.2432]];
    [locationsArray addObject:[[CLLocation alloc] initWithLatitude:28.6020 longitude:77.2478]];
    [locationsArray addObject:[[CLLocation alloc] initWithLatitude:28.5244 longitude:77.1855]];

Now, Take some reference location,

CLLocation *yourLocationA ; //set whatever value you have..

You can sort array of location with following.

 [locationsArray sortUsingComparator:^NSComparisonResult(CLLocation *obj1Location,CLLocation *obj2Location) {
            CLLocationDistance obj1Distance = [obj1Location distanceFromLocation: yourLocationA];
            CLLocationDistance obj2Distance = [obj2Location distanceFromLocation: yourLocationA];

            return (obj1Distance > obj2Distance);

    }];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sort array by distance from current location

How to get distance and location using zipcode in iOS?

How to get the shortest distance from a point on 2d plane from an array of location objects

Is there an efficient way sort an array of objects by distance to a geographic location using coordinates?

How to sort array of objects(latitude, longitude) by nearest distance?

How to get nearest location based on user location from array of lat, long: react, leaflet

How to sort array by distance from coordinates and model

How to get distance between current location and the next current location

Sort array by the distance to a number

How can I sort an array of Business objects by distance from user?

Sort array of points by ascending distance from given

How to Sort Geo-points according to the distance from current location in Android

How to sort an array according to shorted distance Javascript

ElasticSearch - Cant not sort by distance location - "status": 500

How to use loop for retrieve distance between multiple coordinates from array?

iOS: (Swift) How to show distance from current location and exist annotation on annotation subtitle

How to get distance from one origin to multiple destination (Direct distance) with HERE MAP REST API

Angular 6 How to Get Distance between two location AGM

How to get current user latitude and longitude and calculate distance with location B?

How to get the Euclid distance from specified array in numpy?

How to orderBy the closest distance from current location in AngularFire2?

How to display distance and location from JsonObject in textview in android?

How can I find some distance from my location with Swift

How to find Shortest distance of a location from a route (Python),

Find Distance to Nearest Zero in NumPy Array

Shortest Distance From An Array

How to take location in one array to find a element of that location in another array?

Get places around location given a distance

Get distance between two location in java android