如何得到一个点的迟和长



我是iPhone开发新手。我正在设计一个项目,我已经给出了起点和终点地址。假设这两点之间的距离是100英里,然后把20英里分成5等份。那么如何到达离起点20英里远的地方呢?

将此函数添加到您的类中:

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

最新更新