在地图上绘制两点间的线路图
来源:互联网 发布:可控核聚变知乎 编辑:程序博客网 时间:2024/04/28 15:29
原理:用google找到两个经纬度之间的路线点,然后在地图上把点用线连起来,就成了线路图
下面看代码:
currLocation = [[CLLocation alloc] initWithLatitude:34.82373560 longitude:113.67290260]; MKCoordinateRegion region; region.center = currLocation.coordinate; region.span = MKCoordinateSpanMake(0.1, 0.1); MKCoordinateRegion adjustedRegion = [self.myMapView regionThatFits:region]; [self.myMapView setRegion:adjustedRegion animated:YES]; anno = [[BasicMapAnnotation alloc] initWithLatitude:currLocation.coordinate.latitude andLongitude:currLocation.coordinate.longitude]; anno.title = @"当前位置"; [self.myMapView addAnnotation:anno]; annoDes = [[DestAnnotionView alloc] initWithLatitude:34.774621 andLongitude:113.700673]; annoDes.title = @"广播大学"; [self.myMapView addAnnotation:annoDes]; CLLocationCoordinate2D coordinates[2] = {{currLocation.coordinate.latitude, currLocation.coordinate.longitude}, {34.774621, 113.700673}}; locations1 = [self getPointsFrom:coordinates[0] toEnd:coordinates[1]]; NSLog(@"locations = %@",locations1); [self drawLineWithLocationArray:locations1];
- (void)drawLineWithLocationArray:(NSArray *)locationArray{ int pointCount = [locationArray count]; CLLocationCoordinate2D *coordinateArray = (CLLocationCoordinate2D *)malloc(pointCount * sizeof(CLLocationCoordinate2D)); for (int i = 0; i < pointCount; ++i) { CLLocation *location = [locationArray objectAtIndex:i]; coordinateArray[i] = location.coordinate; } MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:pointCount]; [self.myMapView setVisibleMapRect:[routeLine boundingMapRect]]; [self.myMapView addOverlay:routeLine]; free(coordinateArray); coordinateArray = NULL;}#pragma mark mapView delegate functions-(void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{ routeimageView.hidden = YES;}- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{ [self updateRouteView:locations1]; routeimageView.hidden = NO; [routeimageView setNeedsDisplay];}-(NSArray *)getPointsFrom:(CLLocationCoordinate2D)startPoint toEnd:(CLLocationCoordinate2D)destionPoint{ NSString * start = [NSString stringWithFormat:@"%f,%f",startPoint.latitude,startPoint.longitude]; NSString * end = [NSString stringWithFormat:@"%f,%f",destionPoint.latitude,destionPoint.longitude]; NSString * path = [NSString stringWithFormat:@"http://maps.google.com/maps/api/directions/json?origin=%@&destination=%@&sensor=true",start,end]; ASIHTTPRequest * request1 = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:path]]; [request1 startSynchronous]; NSString * data1 = [request1 responseString]; NSDictionary * dic = [data1 objectFromJSONString]; NSArray * dicRouts = [dic valueForKey:@"routes"]; NSDictionary * legs = [dicRouts objectAtIndex:0]; NSDictionary * temp = [legs objectForKey:@"overview_polyline"]; NSMutableString * endpoints = (NSMutableString *)[temp objectForKey:@"points"]; NSArray * arr = (NSArray *)[self decodePolyLine:endpoints]; return arr;}-(NSMutableArray *)decodePolyLine:(NSMutableString *)encoded{ NSInteger len = [encoded length]; NSInteger index = 0; NSMutableArray * array1 = [[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]; CLLocation * loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]]; [array1 addObject:loc]; } NSLog(@"array1 = %@",array1); return array1;}-(void)updateRouteView:(NSArray *)routs{ CGContextRef context = CGBitmapContextCreate(nil, routeimageView.frame.size.width, routeimageView.frame.size.height, 8, 4*routeimageView.frame.size.width, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast); CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 0.0); CGContextSetLineWidth(context, 6.0); for (int i=0; i<routs.count; i++) { CLLocation * location = [routs objectAtIndex:i]; CGPoint point = [self.myMapView convertCoordinate:location.coordinate toPointToView:routeimageView]; if (i==0) { CGContextMoveToPoint(context, point.x, routeimageView.frame.size.height-point.y); } else { CGContextAddLineToPoint(context, point.x, routeimageView.frame.size.height-point.y); } CGContextStrokePath(context); CGImageRef image = CGBitmapContextCreateImage(context); UIImage * image1 = [UIImage imageWithCGImage:image]; routeimageView.image = image1; }}- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{ if ([annotation isKindOfClass:[BasicMapAnnotation class]]) { MKAnnotationView * annocationView = [self.myMapView dequeueReusableAnnotationViewWithIdentifier:@"callAnno"]; if (annocationView == nil) { annocationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"callAnno"]; } annocationView.image = [UIImage imageNamed:@"pin.png"]; annocationView.selected = YES; annocationView.canShowCallout = YES; return annocationView; } if ([annotation isKindOfClass:[DestAnnotionView class]]) { MKAnnotationView * annocationView1 = [self.myMapView dequeueReusableAnnotationViewWithIdentifier:@"callAnno1"]; if (annocationView1 == nil) { annocationView1 = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"callAnno1"]; } annocationView1.image = [UIImage imageNamed:@"pin.png"]; annocationView1.selected = YES; annocationView1.canShowCallout = YES; return annocationView1; } return nil;}-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{ MKPolylineView *view = [[MKPolylineView alloc] initWithOverlay:overlay]; view.strokeColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]; NSArray *pattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:20], [NSNumber numberWithInt: 10], nil]; view.lineDashPattern = pattern; view.lineWidth = 3; return view;}
有心人 慢慢看代码啊,不做过多的解释。
- 在地图上绘制两点间的线路图
- 在iOS地图上绘制两点间路线
- IOS 在iOS地图上绘制两点间路线
- 在iOS地图上绘制两点间路线网友分享
- 在iOS地图上绘制两点间路线
- 在iOS地图上绘制两点间路线
- i os 地图上绘制两点间路线
- 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内。以及两点间的测距功能
- 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内。以及两点间的测距功能
- Android中在地图上显示两点间路径的方法
- Python计算地图上两点经纬度间的距离
- Openlayers3 计算地图上任意两点间的距离
- 计算地图上两点的距离
- 计算地图上两点之间的距离
- 计算地图上两点之间的距离
- 地图上计算两点之间的距离
- iOS 在地图上绘制出你运动的轨迹
- 在地图上绘制你运动的轨迹 ios
- perl的格式化(Format)报表输出
- Protobuf-C For Embedded Developers (and other cross compilers)
- 基本语言细节--《深度探索C++对象模型》--(7)On the Cusp of the Object Model-总结点
- OGR对shp文件的操作
- 几种方法实现线程
- 在地图上绘制两点间的线路图
- C++操作符重载
- matlab查看开源函数源码
- Android实用代码七段(一)
- 黑莓开发
- CRF条件随机场简介
- Windows下的grep命令--findstr
- 用自己电脑做网站服务器-PHPStudy+花生壳
- log4j完全配置