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. UVALive - 4618 Wormholes(负环)

    题目大意:给出出发点和终点和m个虫洞(虫洞的出发点.终点,生成时间和花费时间).问从起点到终点花费的最小时间 解题思路:关键是有负环,所以直接跑最短路算法的话会TLE.所以负环要处理一下 可是这个负环 ...

  2. Spring学习笔记二:注入方式

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6774608.html  我们说,IOC的实现方式是依赖注入,也就是把被依赖对象赋值到依赖对象的成员属性.怎么做 ...

  3. android JNI调用(Android Studio 3.0.1)(转)

    最近回头复习了一下android 的jni调用,却发现按以前的方法调用失败,一怒之下就重新摸索,碰了几次壁,发现网上好多教程都不能成功调用,于是记录一下现在AS版本成功好用的调用方法. 这里设定你的n ...

  4. Java之创建对象>7.Avoid finalizers

    1.Finalizers are unpredictable, often dangerous, and generally unnecessary. 2.never do anything time ...

  5. Java获取本机IP

    try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); whil ...

  6. POJ 2003 Hire and Fire (多重链表 树结构 好题)

    Hire and Fire Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2316   Accepted: 655 Desc ...

  7. V-rep学习笔记:机器人模型创建1—模型简化

    要进行机器人仿真首先需要得到机器人的几何模型.我们可以直接通过VREP中提供的基本几何体来搭建一个简易的机器人[Menu bar --> Add --> Primitive shape - ...

  8. redis实现简单的分布式锁

    在分布式系统中多个请求并发对少数资源进行争抢,例如10个人同时秒杀一件商品,如果不用分布式的锁进行处理(当然还有其它的处理方案),则很容易出现多个人抢到一个商品(超卖)的情况,用redis可以比较容易 ...

  9. python之获取微信access_token

    # -*- coding: cp936 -*- #python 27 #xiaodeng #获取微信access_token #办法一:将该url直接填写到浏览器地址中可以获得access_token ...

  10. ArcGIS 概述

    ArcGIS产品线为用户提供一个可伸缩的,全面的GIS平台.ArcObjects包含了大量的可编程组件,从细粒度的对象(例如,单个的几何对象)到粗粒度的对象(例如与现有ArcMap文档交互的地图对象) ...