0

Hi aim using following method to find out the distance between my current location and the locations(lat and longitude) stored in NSMutableArray value.

But am getting wrong distance..Please help..

My code

    -(void)getDistancetoShow:(NSMutableArray *)newArray{

    CLLocationCoordinate2D coordinateUser = [self getLocation];


    float _lat,_long;


  first_Loc = [[CLLocation alloc] initWithLatitude:coordinateUser.latitude longitude:coordinateUser.longitude];

    for(int p=0;p<[[newArray1 valueForKey:@"Name"] count];p++){

        _lat=[[[newArray1 valueForKey:@"Latitude"] objectAtIndex:p]floatValue];
         _long=[[[newArray1 valueForKey:@"Longitude"] objectAtIndex:p]floatValue];
        second_loc=[[CLLocation alloc] initWithLatitude:_lat longitude:_long];


        showDistance=[second_loc distanceFromLocation:first_Loc]/1000;

        [distanceToDispaly addObject:[NSString stringWithFormat:@"%.2f KM",showDistance]];
    }

     NSLog(@"first=%@, second=%@", first_Loc, second_loc);


}

Latitudes in array

( "47.0735010448824", "47.0564688100431", " 47.0582514311038", "47.0587640538326", "47.0569233603454", "47.0541853132569", "47.0542029215138", "47.0544259594592", "47.0560264547367", " 47.0576532159776", " 47.0550023679218", "47.0342030007379", "47.0746263896213", " 47.0740256635512", "47.0524765957921", "47.0606287049051", "47.0539691521825", "47.0542799159057", "47.0651001682846", "47.0536948902097", "47.0525973335309", "47.0389265414812", "47.0761811267051", "47.0668801601942", "47.0614859079241", "47.0579433468181", "47.0718998779465" )

and longitude in array

( "21.9154175327011", "21.9312065669748", "21.9337414545594", " 21.9346772505188", " 21.9300587945685", "21.9363460105132", "21.9362081709222", "21.9343042603097", "21.939485335992", "21.9320057169724", "21.9300799002643", "21.9485373571669", "21.9310667367526", "21.9318507902135", "21.9192195298473", "21.9195273899529", "21.9329595191441", "21.9292015418841", "21.9219452321208", "21.9098849252041", "21.9074768948561", "21.9424499491422", "21.9151458954504", "21.9304346568769", "21.9305973807911", "21.9331511189507", "21.9159872752442" )

but the real distance in something like staring with 9**** but am getiing now 5***

Bangalore
  • 1,572
  • 4
  • 20
  • 50
  • Do you run your app in simulator? Simulator can't show your current location – Zhans Jan 27 '14 at 07:39
  • What is the value of `first_Loc`? Without that information, it is impossible to check your results. – Martin R Jan 27 '14 at 08:21
  • my lat and long 12.975602--77.638898 – Bangalore Jan 27 '14 at 08:29
  • 1
    Have you *verified* that `first_loc` contains what you expect? - When I compute the distance from `first_loc = (12.975602,-77.638898)` to `second_loc = (47.0735010448824, 21.9154175327011)` with `[second_loc distanceFromLocation: first_loc]` then the result is 9656643.489674237. – Martin R Jan 27 '14 at 08:50
  • yes..but when iam trying using following code am getting like 5623.222KM – Bangalore Jan 27 '14 at 09:45
  • can u please check my above code that,if any mistakes in that code ? – Bangalore Jan 27 '14 at 09:46
  • 1
    @user2990885: Can you please add `NSLog(@"first=%@, second=%@", first_loc, second_loc);` to your code and show the output (only the output for the first calculated distance) ? – Martin R Jan 27 '14 at 10:08
  • first=<+12.97561040,+77.63890489> +/- 0.00m (speed -1.00 mps / course -1.00) @ 27/01/14 4:46:18 pm India Standard Time, second=<+47.06212234,+21.92973518> +/- 0.00m (speed -1.00 mps / course -1.00) @ 27/01/14 4:46:18 pm India Standard Time – Bangalore Jan 27 '14 at 11:16
  • @user2990885: The distance from <+12.97561040,+77.63890489> to <+47.06212234,+21.92973518>, calculated with distanceFromLocation:, is 6382278.067965 meter = 6382 km. I get (almost) the same result with the online calculator http://www.nhc.noaa.gov/gccalc.shtml. So why do you think the result should be 9*** ? Perhaps you mixed up east/west longitude somewhere? – Martin R Jan 27 '14 at 12:12
  • how i sthat possible??? try bangalore to romania distance – Bangalore Jan 27 '14 at 12:22
  • possible duplicate of [How do I calculate distance between two latitude-longitude points?](http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points) – Jave Jan 27 '14 at 15:15
  • 1
    @Jave: It is not a duplicate of that question. As you can see, OP is using the proper method `distanceFromLocation:` to calculate the distance. – Martin R Jan 27 '14 at 17:41
  • @user2990885: Please tell us what result you exactly expand *AND WHY*. At present we cannot reproduce your problem. – Martin R Jan 27 '14 at 17:42
  • @Martin my problem was that distance ,Actually am confused which is correct 9625KM or 6382KM thats my problem – Bangalore Jan 27 '14 at 17:43
  • @user2990885: Why 9625KM? How did you calculate that value? Can you explain that in a way that somebody else can reproduce it? – Martin R Jan 27 '14 at 17:44

3 Answers3

1

CLLocation gives you crow(straight) distance between two places. I thinks you are getting crow distance.

First take coordinate of two places and find distance between them. then search crow distance between those two coordinate.

hope this will help

Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36
  • exact distance is like 96222.00 but it showing only 56232.00 etc – Bangalore Jan 27 '14 at 07:36
  • ok first you should give the hard coded coordinate of two point Take two CLLocation variable and apply distanceFromPlayerToEnemySelectedInMeters = [enemyLocation distanceFromLocation:playerLocation]; – Gajendra Rawat Jan 27 '14 at 08:42
1

Once you get the two coordinates you can calculate the distance between them using this piece of code (taken from here):

- (NSNumber*)calculateDistanceInMetersBetweenCoord:(CLLocationCoordinate2D)coord1 coord:(CLLocationCoordinate2D)coord2 {
    NSInteger nRadius = 6371; // Earth's radius in Kilometers
    double latDiff = (coord2.latitude - coord1.latitude) * (M_PI/180);
    double lonDiff = (coord2.longitude - coord1.longitude) * (M_PI/180);
    double lat1InRadians = coord1.latitude * (M_PI/180);
    double lat2InRadians = coord2.latitude * (M_PI/180);
    double nA = pow ( sin(latDiff/2), 2 ) + cos(lat1InRadians) * cos(lat2InRadians) * pow ( sin(lonDiff/2), 2 );
    double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
    double nD = nRadius * nC;
    // convert to meters
    return @(nD*1000);
}

Hope this helps!

Gad
  • 2,867
  • 25
  • 35
  • CLLocation has a method named `distanceFromLocation:` returns distance in meter – Zhans Jan 27 '14 at 07:42
  • How do you know the real distance? – Gad Jan 27 '14 at 08:17
  • i searched in google it showing distance is 9621KM and iam getting only 5646509.944427(in meter) . When i first implemented my code it was showing like 9621212.245445(meter) but i tried to divide with 1000.after that i removed that diving part but still it giving wrong value – Bangalore Jan 27 '14 at 08:19
  • 2
    Remark: I don't know how `distanceFromLocation:` computes the distance, but the "Haversine Formula" (which is what your `calculateDistanceInMetersBetweenCoord:` uses) cannot be precise for all locations on the earth, because the radius of the earth is not constant. – Martin R Jan 27 '14 at 09:06
  • @MartinR you're correct. Since this is not my code I didn't realise this method has a potential inaccuracy of up to 0.5%. +1 – Gad Jan 27 '14 at 09:17
0

To get distance from array of points in Swift use below reduce method.

Here locations is array of type CLLocation.

 let calculatedDistance = locations.reduce((0, locations[0])) { ($0.0 + $0.1.distance(from: $1), $1)}.0
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71