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,下面 ...
随机推荐
- tcp线程聊天
.ServerThread package serverclient; import java.io.BufferedReader; import java.io.InputStreamReader; ...
- H5 新增内容 新增属性
1.视频 video 2.音频 audio 3.拖放 Drag 和 drop 4.画布 canvas 5.SVG 6.地理定位 navigator.geolocation.getCurrentPosi ...
- JavaWeb项目配置化之Properties类的使用
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6397370.html 在项目中的应用见: https://github.com/ygj0930/Couple ...
- Cas Server源码编译现场实例
最近公司有项目需要做单点登录,根据要求就写下这篇从github上下载的包到项目编译通过,再到修改原代码实现自己的特殊逻辑. 前提: java环境 tomcat环境 maven环境 MyEclipse开 ...
- java 生成二维码、可带LOGO、可去白边
1.准备工作 所需jar包: JDK 1.6: commons-codec-1.11.jar core-2.2.jar javase-2.2.jar JDK 1.7: commons-codec- ...
- LR函数基础(一)(二)
LR函数基础(一) 函数用到:web_reg_find(). lr_log_message(). lr_eval_string().strcmp().atoi() Action(){ web_r ...
- <转>Boost库之asio io_service以及run、run_one、poll、poll_one区别
本文转自:http://blog.csdn.net/byxdaz/article/details/71088812 一.io_service的作用 io_servie 实现了一个任务队列,这里的任务就 ...
- HDUOJ----Coin Change
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Weex学习与实践
Weex学习与实践(一):Weex,你需要知道的事 本文主要介绍包括Weex基本介绍.Weex源码结构.初始化工程.we代码结构.Weex的生命周期.Weex的工作原理.页面间通信.boxmodel ...
- magento直接操作数据库
查: $read = Mage::getSingleton("core/resource")->getConnection('core_read'); $sq ...