使用 API 中的坐标绘制路线
Posted
技术标签:
【中文标题】使用 API 中的坐标绘制路线【英文标题】:Draw route using coordinates from API 【发布时间】:2014-12-16 22:28:27 【问题描述】:我正在使用 transportAPI 获取从一个点到另一个点的路线。我打算在地图视图中选择后绘制路线。选择了一条首选路线,我遍历它的数组以获取所有坐标。
问题是我无法使用 MKPolyline 绘制路线。下面是我的代码;
- (void)viewDidLoad
[super viewDidLoad];
// NSLog(@"selected route: %@",selectedRoute.routeParts);
NSMutableArray * cordArray = [[NSMutableArray alloc]init];
for (int i = 0;i < selectedRoute.routeParts.count; i++)
NSMutableArray * cords =[[selectedRoute.routeParts objectAtIndex:i]objectForKey:@"coordinates"];
for (int c = 0;c < cords.count; c++)
double latitude = [[[cords objectAtIndex:c]objectAtIndex:1]doubleValue];
double longitude = [[[cords objectAtIndex:c]objectAtIndex:0]doubleValue];
CLLocation * location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[cordArray addObject:location];
NSLog(@"coordiantes %@",cordArray);
CLLocationCoordinate2D coordinates[cordArray.count];
//int i = 0;
int numPoints = [cordArray count];
for (int i = 0; i<numPoints;i++)
CLLocation * current = [cordArray objectAtIndex:i];
coordinates[i] = current.coordinate;
// create a polyline with all cooridnates
self.polyline = [MKPolyline polylineWithCoordinates:coordinates count:numPoints];
[self.mapView addOverlay:self.polyline];
[mapView setDelegate:self];
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
if ([overlay isKindOfClass:[MKPolyline class]])
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
renderer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
renderer.lineWidth = 3;
return renderer;
return nil;
坐标数组的sn-p;
oordiantes (
"<+51.51756000,-0.12033000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/14, 10:04:19 PM Greenwich Mean Time"
)
2014-12-16 22:04:19.545 MeetUp[16434:1366070] coordiantes (
"<+51.51756000,-0.12033000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/14, 10:04:19 PM Greenwich Mean Time",
"<+51.51865000,-0.12089000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/14, 10:04:19 PM Greenwich Mean Time"
)
2014-12-16 22:04:19.547 MeetUp[16434:1366070] coordiantes (
"<+51.51756000,-0.12033000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/14, 10:04:19 PM Greenwich Mean Time",
"<+51.51865000,-0.12089000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/14, 10:04:19 PM Greenwich Mean Time",
"<+51.51912000,-0.12129000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 12/16/14, 10:04:19 PM Greenwich Mean Time"
)
我认为返回的坐标应该只是这样,例如:
[+51.51756000,-0.12033000]
[51.51865000,-0.12089000]
[+51.51912000,-0.12129000]
我做错了什么?
【问题讨论】:
【参考方案1】:尝试在添加叠加层之前设置地图的委托,例如:
// create a polyline with all cooridnates
self.polyline = [MKPolyline polylineWithCoordinates:coordinates count:numPoints];
[mapView setDelegate:self]; // <-- move this line to before addOverlay
[self.mapView addOverlay:self.polyline];
至于你的coordiantes
数组日志,是正确的。
【讨论】:
这就像一个魅力,愚蠢的我,谢谢你。如果我想放大这些点,我该如何实现?谢谢 @DrPatience 查看setRegion:animated:
:developer.apple.com/library/mac/documentation/MapKit/Reference/…:以上是关于使用 API 中的坐标绘制路线的主要内容,如果未能解决你的问题,请参考以下文章
使用可观察的地理坐标集合在 WP7 的 bing 地图上绘制路线