0

我是 iPhone 开发人员的新手。我正在设计一个项目,我在其中给出了起点和终点地址。假设我得到这些点之间的距离是 100 英里,我将 20 英里分成五个相等的部分。那么如何获得距起点20英里远的点的经纬度呢?

4

1 回答 1

1

将此功能添加到您的课程中:

// 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;
}

然后您可以像这样使用此方法(假设您要将距离分成 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);
}
于 2011-08-16T08:43:33.307 回答