Finding Path/Route Between two points on MapKit in iPhone
来源:互联网 发布:nginx禁止ip直接访问 编辑:程序博客网 时间:2024/05/12 03:03
http://tricksni.blogspot.com/2012/12/finding-pathroute-between-two-points-on.html
Below is the code to finds path & draws line between two locations.
To implement below class:
_mapRecord = [[PSMapDirection alloc] initWithFrame:CGRectMake(0.0, 49.0, 320.0, 411.0)];[self.view addSubview:_mapRecord];
MapDirection.h
#import <UIKit/UIKit.h>#import <MapKit/MapKit.h>#import "RegexKitLite.h"@interface MapDirection : UIView<MKMapViewDelegate> { MKMapView* mapView; NSArray* routes; BOOL isUpdatingRoutes;}-(void) showRouteFrom: (MKAnnotation*) f to:(MKAnnotation*) t;@end
MapDirection.m
#import "MapDirection.h"@interface MapDirection()-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) from to: (CLLocationCoordinate2D) to;-(void) centerMap;@end- (id) initWithFrame:(CGRect) frame{ self = [super initWithFrame:frame]; if (self != nil) { mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; mapView.showsUserLocation = NO; [mapView setDelegate:self]; [self addSubview:mapView]; } return self;}- (NSMutableArray *)decodePolyLine: (NSMutableString *)encoded { [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])]; NSInteger len = [encoded length]; NSInteger index = 0; NSMutableArray *array = [[NSMutableArray alloc] init]; NSInteger lat=0; NSInteger lng=0; while (index < len) { NSInteger b; NSInteger shift = 0; NSInteger result = 0; do { b = [encoded characterAtIndex:index++] - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1)); lat += dlat; shift = 0; result = 0; do { b = [encoded characterAtIndex:index++] - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1)); lng += dlng; NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5]; NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5]; //printf("[%f,", [latitude doubleValue]); //printf("%f]", [longitude doubleValue]); CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]]; [array addObject:loc]; } return array;}-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t { NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude]; NSString* 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]; //NSLog(@"api url: %@", apiUrl); NSError* error = nil; NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:&error]; NSString *encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L]; return [self decodePolyLine:[encodedPoints mutableCopy]];}-(void) centerMap{ MKCoordinateRegion region; CLLocationDegrees maxLat = -90.0; CLLocationDegrees maxLon = -180.0; CLLocationDegrees minLat = 90.0; CLLocationDegrees minLon = 180.0; for(int idx = 0; idx < routes.count; idx++) { CLLocation* currentLocation = [routes 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) / 2.0; region.center.longitude = (maxLon + minLon) / 2.0; region.span.latitudeDelta = 0.01; region.span.longitudeDelta = 0.01; region.span.latitudeDelta = ((maxLat - minLat)<0.0)?100.0:(maxLat - minLat); region.span.longitudeDelta = ((maxLon - minLon)<0.0)?100.0:(maxLon - minLon); [mapView setRegion:region animated:YES];}-(void) showRouteFrom: (MKAnnotation*) f to:(MKAnnotation*) t { if(routes) { [mapView removeAnnotations:[mapView annotations]]; } [mapView addAnnotation:f]; [mapView addAnnotation:t]; routes = [self calculateRoutesFrom:f.coordinate to:t.coordinate]; NSInteger numberOfSteps = routes.count; CLLocationCoordinate2D coordinates[numberOfSteps]; for (NSInteger index = 0; index < numberOfSteps; index++) { CLLocation *location = [routes objectAtIndex:index]; CLLocationCoordinate2D coordinate = location.coordinate; coordinates[index] = coordinate; } MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps]; [mapView addOverlay:polyLine]; [self centerMap];}#pragma mark MKPolyline delegate functions- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay { MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay]; polylineView.strokeColor = [UIColor purpleColor]; polylineView.lineWidth = 5.0; return polylineView;}@end
Sample Map Route for starting points - "41.967659,-87.627869" and destination points - "41.574361,-91.083069"
0 0
- Finding Path/Route Between two points on MapKit in iPhone
- lintcode:Route Between Two Nodes in Graph
- DFS-----Route Between Two Nodes in Graph
- #176 Route Between Two Nodes in Graph
- Route Between Two Nodes in Graph
- Route Between Two Nodes in Graph
- Android – Distance between two points on the Earth
- Lintcode176 Route Between Two Nodes in Graph solution 题解
- Finding all possible simple paths (paths without cycles) between two vertices in a graph
- How To Get Min-Cost Between two points in graph (Dijkstra’s algorithm)
- 176.Route Between Two Nodes in Graph-图中两个点之间的路线(中等题)
- Two points for Mac in Enterprises
- Finding all paths climbing stairs in one or two steps.
- Cool Scripts 1:Finding Programs in the PATH
- SVN merge between two branches - “path not found”
- two type of parsing date on iPhone
- 4.1 Route Between Nodes
- Android - Draw a path (array of points) in MapView
- 谨记:android编程R.java未自动生成的重要原因之一
- 信号和槽的使用
- 手把手教你怎么用ArcgisOnline发布地图服务
- 新浪云sae上传图片并生成缩略图
- php下intval()和(int)转换使用与区别
- Finding Path/Route Between two points on MapKit in iPhone
- Android:persistentDrawingCache解释
- Ubuntu中完全删除Apache2的办法
- iOS开发的第三方库管理工具cocoapods
- android X86之旅
- Android基础(1)--Activity
- 自己写的一个php分页类
- 跟踪 twisted 里deferred 的Callback
- MathType 6.0 中某些符号不能正确显示