Search code examples
iphonegoogle-mapsmkreversegeocodermapkit

How get lat and long of a point?


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?


Solution

  • 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);
    }