我正在使用谷歌地图ios sdk,我的问题是我必须在地图中的多边形线上获得每1000米的坐标。现在我能够获得给定路径中的位置数,并能够使用以下代码片段访问它们。
-(NSMutableArray*)getCoordinates {
pathCoordinatesArray = [[NSMutableArray alloc]init];
GMSMutablePath *path = [VTDirectionManager getPath];
NSLog(@"count %d",path.count);
for (int i=0 ; i<path.count; i++) {
if (i+1 > path.count) {
return pathCoordinatesArray;
}
for (int j = i+1; j<path.count; j++) {
CLLocationCoordinate2D sourceCoordinate = [path coordinateAtIndex:i];
CLLocation *sourceLocation = [[CLLocation alloc]initWithLatitude:sourceCoordinate.latitude longitude:sourceCoordinate.longitude];
CLLocationCoordinate2D destinationCoordinate = [path coordinateAtIndex:j];
CLLocation *destinationLocation = [[CLLocation alloc]initWithLatitude:destinationCoordinate.latitude longitude:destinationCoordinate.longitude];
BOOL check ;
check = [self checkDistanceForSource:sourceLocation andDestination:destinationLocation];
//jump to next 1000 distance position
if (check) {
i = j;
}
}
}
return pathCoordinatesArray;
}
-(BOOL)checkDistanceForSource:(CLLocation*)source andDestination:(CLLocation*)destination {
CLLocationDistance distance = [source distanceFromLocation:destination];
if (distance > 1000) {
CLLocation *location = [[CLLocation alloc] initWithLatitude:destination.coordinate.latitude longitude:destination.coordinate.longitude];
[pathCoordinatesArray addObject:location];
return YES;
}
return NO;
}
假如我有5000米距离路径,那么我必须得到5个坐标,每个坐标位于1000米位置。
我认为这是错误的代码。建议我使用优化代码
查看图像,每个点都是1000米的距离。
答案 0 :(得分:0)
请更改您的循环代码
for (int i=0 ; i<path.count; i++) {
if (pathCoordinatesArray.count == 0) {
CLLocationCoordinate2D temp2d = [path coordinateAtIndex:i];
CLLocation *tempLoc = [[CLLocation alloc]initWithLatitude:temp2d.latitude longitude:temp2d.longitude];
[pathCoordinatesArray addObject:tempLoc];
}
CLLocationCoordinate2D destinationCoordinate = [path coordinateAtIndex:i];
CLLocation *destinationLocation = [[CLLocation alloc]initWithLatitude:destinationCoordinate.latitude longitude:destinationCoordinate.longitude];
[self checkDistanceForSource:[pathCoordinatesArray lastObject] andDestination:destinationLocation];
}
}