1

Currently I can calculate the distance between two coordinates which are NSStrings.

I am now trying to calculate a list of coordinates which are in a NSMutableArray but I'm stuck.
I somehow need to calculate the distance between all loc or is there a better way of doing this?

for (int i=0; i < self.trackArray.count; i++)
{
    CGFloat latitude  = [[self.trackArray objectAtIndex:0] doubleValue];
    CGFloat longitude = [[self.trackArray objectAtIndex:1] doubleValue];

    CLLocation *loc = [[CLLocation alloc]  initWithLatitude:latitude longitude:longitude];

    CLLocationDistance distance = [loc distanceFromLocation:loc];
    CLLocationDistance kilometers = distance / 1000.0;
    self.distanceString = [NSString stringWithFormat:@"%f", kilometers];
}

Update. An example of how the array looks from the NSLog.

(
"37.335009,-122.032722",
"37.334977,-122.032813",
"37.334948,-122.032921",
"37.334922,-122.033042",
"37.334892,-122.033184",
"37.334858,-122.033344",
"37.334821,-122.033509",
"37.334780,-122.033696"
)

Sorry for not making it clear what Im trying to do, I have an app that tracks a users location and draws a line behind them, above are coordinates that were tracked from the user, I want to find the distance travelled, currently the calculated distance is as the crow fly's which is the first line of the coordinates and the last line. I want to e.g. calculate the distance between the first 2 lines of coordinates then add that distance to the next 2 lines of coordinates etc. So if the user walks in a circle I want to know the distance.

Ernie
  • 89
  • 1
  • 11
  • What is the layout and type of the data in trackArray? Give an example of exactly what you mean by "calculate the distance between all loc". Do you mean the distance of each item in trackArray to some other fixed location X or the distance between each item within the array (distance from item 0 to item 1, distance from item 1 to item 2, etc)? –  Mar 17 '14 at 14:34
  • I'm not 100% sure what you mean when you say "calculate the distance between all loc". Do you mean between each location in the array and every other location? Or between a certain location and the values in the array? etc... Can you explain what it is you are trying to achieve? i.e. finest the nearest location in the array etc... – Fogmeister Mar 17 '14 at 14:36
  • What are you trying to accomplish? Your code above doesn't use your index `i` at all in your for loop, so it will just do the exact same thing every time. Are you trying to find a max distance between any two? Or a min? Or what exactly? Do you want to print out a list of all distances? – Gavin Mar 17 '14 at 14:38
  • @Anna Updated question with your reply. Thanks – Ernie Mar 17 '14 at 14:58
  • i think what you want is " draw a path you walk on" & calculate the distance between start & end point right ? – Pawan Rai Mar 17 '14 at 15:12

1 Answers1

3

Try this:

-(void) test {
  CLLocationDistance totalKilometers = 0;
  for (int i = 0; i < (self.trackArray.count - 1); i++) // <-- count - 1
  {
     CLLocation *loc1 = [self cLLocationFromString:[self.trackArray objectAtIndex:i]];
     CLLocation *loc2 = [self cLLocationFromString:[self.trackArray objectAtIndex:(i + 1)]];

     CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
     CLLocationDistance kilometers = distance / 1000.0;
     totalKilometers += kilometers;
  }
  self.distanceString = [NSString stringWithFormat:@"%f", totalKilometers];
}

-(CLLocation*) cLLocationFromString:(NSString*)string
{
    NSArray *coordinates = [string componentsSeparatedByString:@","];
    CGFloat latitude  = [[coordinates objectAtIndex:0] doubleValue];
    CGFloat longitude = [[coordinates objectAtIndex:1] doubleValue];
    return [[CLLocation alloc]  initWithLatitude:latitude longitude:longitude];
}
bsarr007
  • 1,914
  • 14
  • 14
  • I think this code may have a bug in it. Doesn't this cause a crash if self.trackArray has count of exactly 1? Then you are accessing i+1 at the 0th iteration would be out of the bounds of the array. I think either 1. You do nothing if you only have one coordinate or 2. You have a "last saved" coordinate (if your app has that kind of functionality), and you calculate the distance between the two. – SimplyKiwi May 04 '17 at 22:02
  • As you can see in the loop condition, if self.trackArray has count exactly 1, we will not enter the loop ! – bsarr007 May 05 '17 at 22:53