在iOS地图上绘制两点间路线

您所在的位置:网站首页 excel经纬度绘制地图路线 在iOS地图上绘制两点间路线

在iOS地图上绘制两点间路线

2023-05-28 13:05| 来源: 网络整理| 查看: 265

在iOS地图上绘制两点间路线 转载

mb646b2b3c1359c 2023-05-23 09:15:27 博主文章分类:iOS

文章标签 #pragma 地理位置 git 文章分类 Html/CSS 前端开发

当我们获取了一组地理位置后,可能会想要在地图上绘制这组地理位置信息所包含的路线。

MKMapView提供了addOverlay功能(以及addAnnotation),让我们可以在地图上放一层遮罩。如果要放一组遮罩,可以用addOverlays。

1. #pragma mark - 2. 3. - (void)drawLineWithLocationArray:(NSArray *)locationArray 4. { 5. int pointCount = [locationArray count]; 6. sizeof(CLLocationCoordinate2D)); 7. 8. for (int i = 0; i < pointCount; ++i) { 9. CLLocation *location = [locationArray objectAtIndex:i]; 10. coordinateArray[i] = [location coordinate]; 11. } 12. 13. self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:pointCount]; 14. [self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; 15. [self.mapView addOverlay:self.routeLine]; 16. 17. free(coordinateArray); 18. coordinateArray = NULL; 19. }

MKPolyLine为我们提供了方便绘制多条线段的功能,它实现了MKOverlay协议,但并不能作为遮罩。我们需要实现相应的遮罩代理方法:

1. #pragma mark - MKMapViewDelegate 2. 3. - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay 4. { 5. if(overlay == self.routeLine) { 6. if(nil == self.routeLineView) { 7. self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease]; 8. self.routeLineView.fillColor = [UIColor redColor]; 9. self.routeLineView.strokeColor = [UIColor redColor]; 10. self.routeLineView.lineWidth = 5; 11. } 12. return self.routeLineView; 13. } 14. return nil; 15. }

下面是我的测试代码,用北京的经纬度和杭州的经纬度画线:

1. - (void)drawTestLine 2. { 3. CLLocation *location0 = [[CLLocation alloc] initWithLatitude:39.954245 longitude:116.312455]; 4. CLLocation *location1 = [[CLLocation alloc] initWithLatitude:30.247871 longitude:120.127683]; 5. NSArray *array = [NSArray arrayWithObjects:location0, location1, nil]; 6. [self drawLineWithLocationArray:array]; 7. }

绘制结果如下:

在iOS地图上绘制两点间路线_地理位置

Jason Lee @ Hangzhou

收藏 评论 分享 举报

上一篇:IOS应用开发版本控制工具之Versions 和SVN 的 使用方法详解

下一篇:iOS 图片左右反转 反向



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3