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,下面 ...
随机推荐
- 〖Linux〗Kubuntu文件管理器单例的设置(即:一个工作区只一个文件管理器)
有没有一种,情况: 1. 程序A打开了文件管理器: 2. 程序B又打开了文件管理器: 导致开了两个文件管理器,太不舒服了: 搜索下 kubuntu dolphin single instance,果然 ...
- 【Windows】XShell中使用小键盘和ALT键(作Meta键),使BackSpace正常
小键盘: 打开终端的Session属性,VT模式,初始数字键盘模式,设置为普通 ALT键: 打开终端的Session属性,元(Meta)键仿真,将ALT用作Meta键 BackSpace: 打开终端的 ...
- FTP下载工具
开源的FTP下载工具,FTP搬运工.... 01.FileZilla_3.21.0_win64 官方地址: https://filezilla-project.org/ 下载地址: http://pa ...
- 转:折腾一晚上Bullet及Ogre相关工具的成果 -- 3Ds Max,Maya, blender, GameKit
起始目的很简单,整合Bullet及Ogre,找个能够生成.scene和.bullet文件的建模工具. 折腾一晚上Bullet及Ogre相关的东西,基本上就像爱迪生发明灯泡一样,得出了N个失败的教训,总 ...
- spring事务配置的两种方式
spring所有的事务管理策略类都继承自org.springframework.transaction.PlatformTransactionManager接口. <!-- 事务管理器配置,单数 ...
- asiHttpRequst 超时代码判断
- (void)requestFailed:(ASIHTTPRequest *)request{ NSDictionary *userInfo = [request userInfo]; id del ...
- ubuntu 终端中文显示乱码问题!
1 Alt+Ctrl+F1 进入第一个终端,发现中文乱码. 2 安装zhcon. sudo apt-get install zhcon 3 输入下面命令,启动zhcon,中文显示正常. zhcon - ...
- ios持久化存储
前言 iOS中常用的持久化存储方式有好几种: 偏好设置(NSUserDefaults) plist文件存储 归档 SQLite3 Core Data 沙盒 每个iOS应用都有自己的应用沙盒(应用沙盒就 ...
- 【虚拟化系列】VMware vSphere 5.1 网络管理
网络是VMware vSphere 5.1的基础,所有虚拟机都需要网络来进行通信.如果将所有的虚拟机都看成是物理机,则在网络拓扑上,需要网卡和交换机等不同的网络连接设备和方式.而在虚拟化中,这些 ...
- Spring使用内存数据库
有时候为了做些测试需要创建数据库及相关表,安装MySQL等轻量数据库虽然简单但还是有点麻烦?而且用于自己临时测试的数据库对象一般不会被多次使用,还是浪费?内存数据库结合ORM可以很好解决这个问题. H ...