IOS7  比较简单

CLLocationCoordinate2D  _start2D;

CLLocationCoordinate2D  _end2D;

NSArray *_routes;

IOS6

需要文件  ....google

#import "RegexKitLite.h"

UIImageView *  _routeView;

NSArray *_routes;

load

   if (IOS7) {
//      [_mapView setRegion:MKCoordinateRegionMake(_start2D, MKCoordinateSpanMake(0.05, 0.05))];_routes = [self calculateRoutesFrom:_start2D to:_end2D] ;[self centerMap:_routes];//    //规划地图路径
       [self requestRoutWithSoure:_start2D Destination:_end2D];}else{_routeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _mapView.frame.size.width, _mapView.frame.size.height)];_routeView.userInteractionEnabled = NO;[_mapView addSubview:_routeView];if(_routes) {[_mapView removeAnnotations:[_mapView annotations]];}_routes = [self calculateRoutesFrom:_start2D to:_end2D] ;//根据数组画线
        [self updateRouteView:_routes RouteView:_routeView];//根据线路确定呈现范围
        [self centerMap:_routes];}

ios7路径规划

#pragma mark - 请求路径
-(void)requestRoutWithSoure:(CLLocationCoordinate2D) start2D  Destination:(CLLocationCoordinate2D)end2D
{MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc] init];directionsRequest.transportType = MKDirectionsTransportTypeAutomobile;MKPlacemark *startMark = [[MKPlacemark    alloc] initWithCoordinate:start2D addressDictionary:nil];MKPlacemark *endMark = [[MKPlacemark    alloc] initWithCoordinate:end2D addressDictionary:nil];MKMapItem *startItem = [[MKMapItem alloc] initWithPlacemark:startMark];MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endMark];[directionsRequest setSource:startItem];[directionsRequest setDestination:endItem];MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];//接收rout[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {if (!response) {return ;}_routes  = [response routes];[_routes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {MKRoute *rout = obj;MKPolyline *line = [rout polyline];[_mapView addOverlay:line];}];//根据 routes  数据 确定region的呈现范围//[self centerMap:_routes];
}];
}

ios7 回调

#pragma mark - ios7  mapView delegate
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id<MKAnnotation>)annotation {MKAnnotationView *annView;if (annotation == _startPoint) {//标注annView = [[MKAnnotationView alloc] initWithAnnotation:_startPoint reuseIdentifier:@"_start2D"];annView.image= [UIImage imageNamed:@"start_location"];}if (annotation == _endPoint) {annView = [[MKAnnotationView alloc] initWithAnnotation:_endPoint reuseIdentifier:@"_end2D"];annView.image= [UIImage imageNamed:@"end_location"];}return annView;
}-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];renderer.strokeColor = lineColor;renderer.lineWidth = lineW;return  renderer;
}

IOS6 路径规划 画线

#pragma mark - ios6 mapView-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {NSString* saddr;NSString* daddr;saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude];NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr];NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:nil];NSString *encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];return [self decodePolyLine:[encodedPoints mutableCopy]:f to:t];
}
//解码
-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded :(CLLocationCoordinate2D)f to: (CLLocationCoordinate2D) t {[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"options:NSLiteralSearchrange:NSMakeRange(0, [encoded length])];NSInteger len = [encoded length];NSInteger index = 0;NSMutableArray *array = [[NSMutableArray alloc] init];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];NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue]longitude:[longitude floatValue]];[array addObject:loc];}CLLocation *first = [[CLLocation alloc] initWithLatitude:[[NSNumber numberWithFloat:f.latitude] floatValue]longitude:[[NSNumber numberWithFloat:f.longitude] floatValue] ];CLLocation *end = [[CLLocation alloc] initWithLatitude:[[NSNumber numberWithFloat:t.latitude] floatValue]longitude:[[NSNumber numberWithFloat:t.longitude] floatValue] ];[array insertObject:first atIndex:0];[array addObject:end];return array;
}
//用routes数组的内容 确定region的呈现范围
-(void) centerMap:(NSArray *)routesArr {MKCoordinateRegion region;CLLocationDegrees maxLat = -90;CLLocationDegrees maxLon = -180;CLLocationDegrees minLat = 90;CLLocationDegrees minLon = 180;for(int idx = 0; idx < routesArr.count; idx++){CLLocation* currentLocation = [routesArr objectAtIndex:idx];if(currentLocation.coordinate.latitude > maxLat)maxLat = currentLocation.coordinate.latitude;if(currentLocation.coordinate.latitude < minLat)minLat = currentLocation.coordinate.latitude;if(currentLocation.coordinate.longitude > maxLon)maxLon = currentLocation.coordinate.longitude;if(currentLocation.coordinate.longitude < minLon)minLon = currentLocation.coordinate.longitude;}region.center.latitude     = (maxLat + minLat) / 2;region.center.longitude    = (maxLon + minLon) / 2;region.span.latitudeDelta  = maxLat - minLat;region.span.longitudeDelta = maxLon - minLon;[_mapView setRegion:region animated:YES];
}//用routes数组的内容  在 UIImageView 上画线
-(void) updateRouteView:(NSArray *)routesArr RouteView:(UIImageView *)routeImg {CGContextRef context = CGBitmapContextCreate(nil,routeImg.frame.size.width,routeImg.frame.size.height,8,4 * routeImg.frame.size.width,CGColorSpaceCreateDeviceRGB(),kCGImageAlphaPremultipliedLast);CGContextSetStrokeColorWithColor(context, lineColor.CGColor);CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);CGContextSetLineWidth(context, lineW);for(int i = 0; i < routesArr.count; i++) {CLLocation* location = [routesArr objectAtIndex:i];CGPoint point = [_mapView convertCoordinate:location.coordinate toPointToView:routeImg];if(i == 0) {CGContextMoveToPoint(context, point.x, routeImg.frame.size.height - point.y);} else {CGContextAddLineToPoint(context, point.x, routeImg.frame.size.height - point.y);}}CGContextStrokePath(context);CGImageRef image = CGBitmapContextCreateImage(context);UIImage* img = [UIImage imageWithCGImage:image];routeImg.image = img;CGContextRelease(context);}#pragma mark mapView delegate functions
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{

if (!IOS7)

    _routeView.hidden = YES;
}- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{

if (!IOS7){

    [self updateRouteView:_routes RouteView:_routeView];_routeView.hidden = NO;[_routeView setNeedsDisplay];}
}

转载于:https://www.cnblogs.com/DamonTang/p/3628752.html

IOS6 IOS7 Mapkit draw Rout(地图划线)相关推荐

  1. 比量iOS6/iOS7, 3.5inch/4.0inch

    Retina (3.5/4 inch Screen) or Non-Retina 比量 if ([[UIScreen mainScreen] respondsToSelector:@selector( ...

  2. 判断iOS6/iOS7, 3.5inch/4.0inch

    Retina (3.5/4 inch Screen) or Non-Retina 判断 if ([[UIScreen mainScreen] respondsToSelector:@selector( ...

  3. OpenLayers实战(七)地图划线LineString,多个点图标

    连接地图上的feature 准备多个用于连线的测试数据点 // 可以通过监听地图click事件,获取地图点击时候的坐标,用作测试数据 map.on('singleclick', e => {co ...

  4. 支持ios6,ios7桌面图片icon的尺寸和命名

    I submitted an app update, but have received an email telling me the above error has occurred. Has a ...

  5. html转义es,Js特殊字符转义之htmlEscape()方法

    为了防止XSS攻击,常常需要将用户输入的特殊字符进行转义,原生js貌似还没有直接对其专业的方法,最近再读Js高级程序设计的时候刚好看到,碰巧项目中也刚好需要使用次方法,于是就之家搬来用了. 网上关于转 ...

  6. (ios7) 解决代码布局View, ios7 中 subView 高度增加StatusBar20dp的问题,保证Ios6,ios7代码一致...

    在ios7 布局中,Status Bar 和 ToolBar ,NavigateBar 等都包含在ViewControl的主View中. 这样原来ios6 的View布局 整体向上移动了20dp,下面 ...

  7. iOS地图之MapKit框架

    MapKit框架在地图中用于显示,工程实例: 1 #import "ViewController.h" 2 3 #import <MapKit/MapKit.h> 4 ...

  8. 转-iOS开发系列--地图与定位

    来自: http://www.cnblogs.com/kenshincui/p/4125570.html#autoid-3-4-0 概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功 ...

  9. iOS开发--地图与定位

    iOS开发--地图与定位 概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们 ...

最新文章

  1. JS URL 编码 PHP 解码{%u5F00%u53D1}
  2. 降维打击:这款GAN可以让真人「二次元化」
  3. 早该改了,只是我们太穷了
  4. 利剑无意之scala小考核
  5. 阿里云2020财年营收超400亿 同比增长62%
  6. click() bind() live() delegate()区别 1
  7. 500+ 精选 Java 面试题大放送
  8. 【iOS-Cocos2d游戏开发之八】开启高清(960*640)模式问题与解答、图片适配以及设置iphone横竖屏...
  9. oracle extract类型,Oracle中extract()函数
  10. [学习官方例子]TCustomComparer
  11. C#捕获摄像头进行拍照和录像资料总结
  12. ubuntu下安装opencv2
  13. 谷歌-adblock插件下载后的使用(国内无需fan墙)
  14. 怎么把pdf用abobe转换成html,Adobe Acrobat:把网页转换为PDF
  15. 各大搜索引擎站点提交入口大全
  16. ios14描述文件无法与服务器连接,iOS14屏蔽更新描述文件已损坏,无法安装的解决办法...
  17. android 关闭第三方应用,Android禁用第三方应用
  18. 练习时长一年半,算法蒟蒻的成长记录
  19. toft 测试用例rat_测试案例如何区分RAT,FAST,TOFT,FET | 学步园
  20. excel单元格斜线_个人永久性免费-Excel催化剂功能第74波-批量排版格式利器,瞬间美化表格...

热门文章

  1. 从cocos看国内自研的游戏引擎
  2. 视觉SLAM总结——视觉SLAM面试题汇总
  3. 使用科大迅飞语音开发时的一系列问题及解决办法
  4. MYSQL注入(Welcome Dhakkan)
  5. 英语中“万”的奇特表示法
  6. Mac电脑如何设置色彩滤镜?
  7. USB之ch376s数据手册
  8. 剑与轮回辅助升级脚本工具 职业选择推荐
  9. Morgan Stanley(摩根士丹利)电面过程
  10. 12个非常不错的免费HTML后台管理模板