使用MKPolyline绘制两个位置之间的路径
我试图在本教程的帮助下显示两个位置之间的路线。 他们使用了坐标的CSV文件,我使用谷歌api来获取坐标。 但结果完全不同。
你可以看到它没有绘制正确的路径。 Plz向我提出了一些建议。
你需要解码你从响应中得到的折线..为了你需要谷歌的算法……
// http://code.google.com/apis/maps/documentation/utilities/polylinealgorithm.html // -(NSMutableArray *)decodePolyLine:(NSString *)encodedStr { NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]]; [encoded appendString:encodedStr]; [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])]; NSInteger len = [encoded length]; NSInteger index = 0; NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease]; NSInteger lat=0; NSInteger lng=0; while (index < len) { NSInteger b; NSInteger shift = 0; NSInteger result = 0; do { b = [encoded characterAtIndex:index++] - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1)); lat += dlat; shift = 0; result = 0; do { b = [encoded characterAtIndex:index++] - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1)); lng += dlng; NSNumber *latitude = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease]; NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease]; // printf("[%f,", [latitude doubleValue]); // printf("%f]", [longitude doubleValue]); CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease]; [array addObject:loc]; } [encoded release]; return array; }
这将为您提供包含所有点的可变数组(以CLLocation对象的forms),也不仅仅解码主折线..解码您收到的每个子折线,然后绘制否则方向将不正确。
您可以通过在代码中添加此简单方法来添加实线(Path):
-(void)addSolidLine:(CLLocation *)source andDestination:(CLLocation*)destination { CLLocationCoordinate2D coordinates[2] = {source.coordinate, destination.coordinate}; MKGeodesicPolyline *geodesicPolylineSolid = [MKGeodesicPolyline polylineWithCoordinates:coordinates count:2]; [self.mapView addOverlay:geodesicPolylineSolid]; } #pragma mark - map view delegate - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id )overlay { if ([overlay isKindOfClass:[MKPolyline class]]) { MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:(MKPolyline *)overlay]; renderer.lineWidth = 1.5f; renderer.strokeColor = [UIColor greenColor]; renderer.alpha = 50; return renderer; } }
如果你想显示虚线,你可以添加这个方法:
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:(MKPolyline *)overlay]; renderer.lineDashPattern = @[@2, @5]; renderer.lineWidth = 1.5f; renderer.strokeColor = [UIColor redColor]; renderer.alpha = 50; isShowingDottedLine = false; return renderer;
- iOS 7.1:当应用程序在后台时,获取Core Motion数据(加速度计,陀螺仪)
- MPMoviePlayerController:我什么时候才能知道文件的下载量达到了10%?
- 将UIImage转换为CVImageBufferRef
- applicationDidFinishLaunching:是否在第一次更新和启动应用程序时被调用?
- 他们如何制作那些奇特的闪屏?
- Modal UIViewController总是在iPad上全屏显示。 为什么?
- 获得interactivePopGestureRecognizerclosurescallback/事件
- 苹果推送通知与定期蜂鸣声
- 有没有人有他们的iOS应用程序被拒绝的苹果使用设备的UDID?