我是iPhone开发人员的新手。我正在设计一个项目,其中我给出了起点和终点地址。假设我得到这些点之间的距离是100英里,我划分为20英里的五个相等的部分。那么距离起点20英里的距离是多少?
答案 0 :(得分: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);
}