0

I am new for iPhone developer. I am designing a project in which i have given starting and ending point address. Suppose i get distance between those points is 100 miles and i divide in five equal parts of 20 miles. Then how get lat and long of point far 20 miles from starting point?

iRam11
  • 309
  • 4
  • 16
  • you have to store all the point and after that fine distance beteen that two points if that is equal to you limit then use it. because coreLocation framework give facility of distance filter. – priyanka Aug 16 '11 at 07:08
  • @priyanka I will give start and end points and then calculate distance between them. After that divide. And i want to get lat and long of points which i get from division of total distance. – iRam11 Aug 16 '11 at 07:16

1 Answers1

1

Add this function to your class:

// position is a number between 0 and 1, where 0 gives the start position, and 1 gives the end position
- (CLLocationCoordinate2D)pointBetweenStartPoint:(CLLocationCoordinate2D)startPoint endPoint:(CLLocationCoordinate2D)endPoint position:(float)position {

    CLLocationDegrees latSpan = endPoint.latitude - startPoint.latitude;
    CLLocationDegrees longSpan = endPoint.longitude - startPoint.longitude;

    CLLocationCoordinate2D ret = CLLocationCoordinate2DMake(startPoint.latitude + latSpan*position,
                                                            startPoint.longitude + longSpan*position);

    return ret;
}

You can then use this method like this (assuming you want to split the distance into 5):

CLLocationCoordinate2D startPoint = CLLocationCoordinate2DMake(/* lat, long of start point */);
CLLocationCoordinate2D endPoint = CLLocationCoordinate2DMake(/* lat, long of end point */);

for (int i = 1; i < 5; i++) {
    float position = i / 5.0;
    CLLocationCoordinate2D middlePosition = [self pointBetweenStartPoint:startPoint endPoint:endPoint position:position];
    NSLog("%f, %f", middlePosition.latitude, middlePosition.longitude);
}
joerick
  • 16,078
  • 4
  • 53
  • 57
  • note that this will only be accurate to small distances, especially if you're close to the poles. – joerick Aug 16 '11 at 10:58
  • CLLocationCoordinate2D middlePosition = [self pointBetweenStartPoint:startPoint endPoint:endPoint position:position]; – iRam11 Aug 16 '11 at 11:02
  • have you added the above function to the class and declared it in your header file? – joerick Aug 16 '11 at 15:54