IOS6 IOS7 Mapkit draw Rout(地图划线)
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(地图划线)的更多相关文章
- 比量iOS6/iOS7, 3.5inch/4.0inch
Retina (3.5/4 inch Screen) or Non-Retina 比量 if ([[UIScreen mainScreen] respondsToSelector:@selector( ...
- iOS- <项目笔记>iOS6 & iOS7屏幕图片适配
1.为非视网膜\视网膜屏幕分别准备2份图片,比如: 1> 非视网膜 abc.png 2> 视网膜 abc@2x.png 程序检测视网膜屏到会自动替换@2x 2.程序启动图片 * 程序启动过 ...
- MapKit地图划线
只要用于获取用户位置都要取得用户授权 #import "ViewController.h" #import <MapKit/MapKit.h> @interface V ...
- iOS6 / iOS7 状态栏高度适配
问题原因:iOS7的状态栏(status bar)不再占用单独的20px,所以假设你在iOS6上的界面布局是正常的,那么到了iOS7上就会变成以下这个样子: 左边是iOS6界面 ...
- ios6,ios7,ios7.1下设置UISearchbar的背景色
ios系统升级到7.1后,原来在7.0下显示正常的UISearchbar现在又出现问题了.究其原因,是由于UISearchbar的subview又做修改了. float version = [[[ U ...
- ios6,ios7强制转屏
在父视图控制器里面写如下代码 -(void)setViewOrientation:(UIInterfaceOrientation )orientation { if ([[UIDevice curre ...
- iOS开发——高级篇——地图 MapKit
一.简介 1.在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆.找KTV.找电影院等等导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达 在上述应用中,都用到了定位 ...
- Swift - 使用MapKit显示地图,并在地图上做标记
通过使用MapKit可以将地图嵌入到视图中,MapKit框架除了可以显示地图,还支持在地图上做标记. 1,通过mapType属性,可以设置地图的显示类型 MKMapType.Standard :标准地 ...
- (ios7) 解决代码布局View, ios7 中 subView 高度增加StatusBar20dp的问题,保证Ios6,ios7代码一致
在ios7 布局中,Status Bar 和 ToolBar ,NavigateBar 等都包含在ViewControl的主View中. 这样原来ios6 的View布局 整体向上移动了20dp,下面 ...
随机推荐
- 对2个hex(16进制)字符串进行异或操作
private static String hexXOR(String hex1, String hex2){ BigInteger i1 = new BigInteger(hex1, 16); Bi ...
- jqPlot图表插件学习之数据节点高亮和光标提示
一.准备工作 首先我们需要到官网下载所需的文件: 官网下载(笔者选择的是jquery.jqplot.1.0.8r1250.zip这个版本) 然后读者需要根据自己的情况新建一个项目并且按照如下的方式加载 ...
- dubbo实现动态调用
在支付网关的业务中,在用户支付成功/失败后需要异步通知业务方,这个时候业务方是一个变数,支付网关可以定义一个interface,各个业务方来实现这个接口. 支付网关配置一个回调配置表,表中包含:gro ...
- django之创建第4-1个项目-访问dict数据
1.修改index.html文件 <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- 转:Irrlicht 0.1引擎源码分析与研究(一)
目录(?)[-] 主要技术特性 引擎概览 Irrlicht的窗口管理 Irrlicht引擎主要是由一个名叫Nikolaus Gebhardt奥地利人所设计,是sourceforge上的一个开源项目 ...
- JavaScript 风格指南
来源于: https://github.com/alivebao/clean-code-js 目录 介绍 变量 函数 对象和数据结构 类 测试 并发 错误处理 格式化 注释 介绍 作者根据 Rober ...
- JavaScript调用App原生代码(iOS、Android)通用解决方案
实际场景 场景:现在有一个H5活动页面,上面有一个登陆按钮,要求点击登陆按钮以后,唤出App内部的登录界面,当登录成功以后将用户的手机号返回给H5页面,显示出来.这个场景应该算是比较完整的一次H5中的 ...
- unix环境高级编程 读书笔记
1.上班业余时间把书下载下来,第一章读完了,但是程序只能回家运行啦!Fighting!
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 【转】GDB中应该知道的几个调试方法
文章来源:http://coolshell.cn/articles/3643.html GDB中应该知道的几个调试方法 2011年02月10日 陈皓 评论 40 条评论 70,776 人阅读 七.八 ...