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(, , _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:NSLiteralSearch
range:NSMakeRange(, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = ;
NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger lat=;
NSInteger lng=;
while (index < len) {
NSInteger b;
NSInteger shift = ;
NSInteger result = ;
do {
b = [encoded characterAtIndex:index++] - ;
result |= (b & 0x1f) << shift;
shift += ;
} while (b >= 0x20);
NSInteger dlat = ((result & ) ? ~(result >> ) : (result >> ));
lat += dlat;
shift = ;
result = ;
do {
b = [encoded characterAtIndex:index++] - ;
result |= (b & 0x1f) << shift;
shift += ;
} while (b >= 0x20);
NSInteger dlng = ((result & ) ? ~(result >> ) : (result >> ));
lng += dlng;
NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-];
NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-];
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:];
[array addObject:end]; return array;
}
//用routes数组的内容 确定region的呈现范围
-(void) centerMap:(NSArray *)routesArr {
MKCoordinateRegion region; CLLocationDegrees maxLat = -;
CLLocationDegrees maxLon = -;
CLLocationDegrees minLat = ;
CLLocationDegrees minLon = ;
for(int idx = ; 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) / ;
region.center.longitude = (maxLon + minLon) / ;
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,
,
* 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 = ; i < routesArr.count; i++) {
CLLocation* location = [routesArr objectAtIndex:i];
CGPoint point = [_mapView convertCoordinate:location.coordinate toPointToView:routeImg]; if(i == ) {
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];
}
}

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. iOS- <项目笔记>iOS6 & iOS7屏幕图片适配

    1.为非视网膜\视网膜屏幕分别准备2份图片,比如: 1> 非视网膜 abc.png 2> 视网膜 abc@2x.png 程序检测视网膜屏到会自动替换@2x 2.程序启动图片 * 程序启动过 ...

  3. MapKit地图划线

    只要用于获取用户位置都要取得用户授权 #import "ViewController.h" #import <MapKit/MapKit.h> @interface V ...

  4. iOS6 / iOS7 状态栏高度适配

    问题原因:iOS7的状态栏(status bar)不再占用单独的20px,所以假设你在iOS6上的界面布局是正常的,那么到了iOS7上就会变成以下这个样子:             左边是iOS6界面 ...

  5. ios6,ios7,ios7.1下设置UISearchbar的背景色

    ios系统升级到7.1后,原来在7.0下显示正常的UISearchbar现在又出现问题了.究其原因,是由于UISearchbar的subview又做修改了. float version = [[[ U ...

  6. ios6,ios7强制转屏

    在父视图控制器里面写如下代码 -(void)setViewOrientation:(UIInterfaceOrientation )orientation { if ([[UIDevice curre ...

  7. iOS开发——高级篇——地图 MapKit

    一.简介 1.在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆.找KTV.找电影院等等导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达 在上述应用中,都用到了定位 ...

  8. Swift - 使用MapKit显示地图,并在地图上做标记

    通过使用MapKit可以将地图嵌入到视图中,MapKit框架除了可以显示地图,还支持在地图上做标记. 1,通过mapType属性,可以设置地图的显示类型 MKMapType.Standard :标准地 ...

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

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

随机推荐

  1. 亲历H5移动端游戏微信支付接入及那些坑(三)——支付接入

    终于到接入支付了,小小的一个微信支付,居然也写了3篇,好长,好累. 接入环境 对接入环境,前端的话,应该是以js为主吧,也有可能是,PHP,Java,C++,或者C#都可以.为什么在此特意提一下接入环 ...

  2. memcache使用方法测试

    <?php //php操作memcache的使用测试总结--学习 //1 Memcache::connect; //$memcache = new Memcache; //$memcache-& ...

  3. mongobooster 的使用

    mongobooster是mongodb的客户端工具 1.配置数据库 file->connect..->from URL 2.数据查询 选中数据库名,右击-Open Shell->输 ...

  4. mysql导入source注意点

    mysql的导入导出要注意字符集,防止查询乱码! 导入前设置字符集 set names utf8; //导入指定编码source /xxx.sqlcommit;

  5. 高密度WIFI部署要点

    1. 划分AP组,分组带宽控制 根据区域的人数密集程度划分不同的AP组,并进行优化策略调整,分组分权限进行带宽控制,以确保单用户的2.4G带宽不低于1M,5G用户不低于2M2. 相邻AP错开信道 超高 ...

  6. 微信小程序支付(java后端)

      第一步 进入小程序,下单,请求下单支付,调用小程序登录API来获取Openid(https://mp.weixin.qq.com/debug/w ... .html#wxloginobject), ...

  7. 初次使用Microsoft Azure

    一.介绍 在微博上偶然发现微软的Azure有免费申请试用的机会,于是赶快给微软发邮件申请,第二天就通过了. 早就听说过微软在云计算方面发力,但一直没机会试用,之前用过国产的BAE.SAE,用GoAge ...

  8. Linux-使用ctags、vim查看数据类型所在头文件

    安装 ctags yum -y install ctags 生成 tags 文件 cd /usr/include ctags -R . 在 vim 中配置 tags #修改 vim 配置文件.或者使用 ...

  9. 架构-到底什么时候该使用MQ【转】

    点击:<查看原文> 一.缘起 一切脱离业务的架构设计与新技术引入都是耍流氓. 引入一个技术之前,首先应该解答的问题是,这个技术解决什么问题. 就像微服务分层架构之前,应该首先回答,为什么要 ...

  10. 理解iOS与函数式编程

    有时候,一个关键字就是一扇通往新世界的大门.两年前,身边开始有人讨论函数式编程,拿关键字Functional Programming一搜,全是新鲜的概念和知识,顺藤摸瓜,看到的技术文章和框架也越来越多 ...