【问题标题】:to view route between two points in map +iphone+Mapkit查看地图中两点之间的路线+iphone+Mapkit
【发布时间】:2011-10-10 21:31:45
【问题描述】:

我想获取地图包中两点之间的路线! 我做了一些谷歌工作,发现了这个:-http://spitzkoff.com/craig/?p=108

我曾用它来显示地图和图钉:-

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden=NO;
    self.title=@"Map";  
    CLLocationCoordinate2D location; 
    location.latitude=[latLabel doubleValue];
    location.longitude=[longLabel doubleValue]; 
    MKCoordinateRegion region;// = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake((location.latitude) / 2.0, (location.longitude) / 2.0), 1000.0, 1000.0);
    region.center=location;
    region.span.latitudeDelta =0.1;
    region.span.longitudeDelta =0.1;
    addAnnotation=[[AddressAnnotation alloc] initWithCoordinate:location];      
    NSLog(@"city=mapview=>%@",City);
    NSLog(@"adress=mapview=>%@",Address);   
    addAnnotation.title=City;
    addAnnotation.subtitle=Address;
    [mapView addAnnotation:addAnnotation];  
    [mapView setRegion:region animated:YES];        
    }

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(AddressAnnotation *) annotation
{
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.pinColor=MKPinAnnotationColorRed;   
    annView.draggable=TRUE; 
    annView.animatesDrop=TRUE;
    annView.canShowCallout=YES;
    annView.calloutOffset=CGPointMake(-5,5);
    annView.draggable=TRUE;
    return annView;
}

和注释类来显示名称和地址。但是我应该如何添加更多来显示两点之间的路线。

请帮帮我。

提前致谢

【问题讨论】:

    标签: iphone android-mapview


    【解决方案1】:

    您需要调用 google 的方向服务。阅读文档

    http://code.google.com/apis/maps/documentation/directions/

    使用适合您需要的 URL。然后你需要一个 JSON 或 XML 解析器。 JSON有一个API,你可以在这里下载

    https://github.com/stig/json-framework/

    然后您可以使用以下方法获取路线折线点:

    if (error == nil) {
            NSError *JSONError = nil;
            SBJsonParser *JSONParser = [[SBJsonParser alloc] init];
            id  directions = [JSONParser objectWithString:directionsInJSON error:&JSONError];
            if (JSONError == nil) {
                directions = (NSDictionary*)directions;
                //NSLog(@"Directions: %@",directions);
    
                NSString *status = [directions objectForKey:@"status"];
                if ([status isEqualToString:@"OK"]) {
                    gotDirections = YES;
                    NSArray *routes = [directions objectForKey:@"routes"];
                    NSDictionary *route = [routes objectAtIndex:0];
                    NSDictionary *polylineOverview = [route objectForKey:@"overview_polyline"];
    
                    NSString *polylinePoints = [polylineOverview objectForKey:@"points"];
                    NSArray *decodedPolyline = [self decodePolyLine:polylinePoints];
                }
            }
    }
    //handle elses
    
    - (NSMutableArray *)decodePolyLine: (NSString *)encoded {
        NSInteger len = [encoded length];
        NSInteger index = 0;
        NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
        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] autorelease];
            NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
            CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease];
            [array addObject:loc];
        }
    
        return array;
    }
    
    Then display this array using mkpolyline:
    
    CLLocationCoordinate2D coords[[routes count]];
            for(int i = 0; i < route.count; i++) {        
                CLLocation* location = (CLLocation*)[NSKeyedUnarchiver unarchiveObjectWithData:[routes objectAtIndex:i]];         
                CLLocationCoordinate2D c;
                c.latitude = location.coordinate.latitude;
                c.longitude = location.coordinate.longitude;        
                coords[i] = c; 
    
                Annotation *ann = [[Annotation alloc] initWithTitle:@"" Subtitle:@"" andCoordinate:c];
                [mapView addAnnotation:ann];
                [ann release];
            }
    
            MKPolyline *line = [MKPolyline polylineWithCoordinates:(CLLocationCoordinate2D*)coords count:[route count]];
            [self.mapView addOverlay:line];
    

    同时实现 mapView:viewForOverlay 委托方法:

    - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
    
        if ([overlay isKindOfClass:[MKPolyline class]]) {
    
            MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
            polylineView.strokeColor = [UIColor redColor];
            polylineView.lineWidth = 1.5;
            return polylineView;
        }
    
        return [[MKOverlayView alloc] initWithOverlay:overlay];
    }
    

    【讨论】:

    • 您好,感谢您提供此解决方案!它对我很有用,有一点细节......它崩溃了:P。在这一行: CLLocation* location = (CLLocation*)[NSKeyedUnarchiver unarchiveObjectWithData:[routes objectAtIndex:i]]; .错误日志显示“[CLLocation bytes]: unrecognized selector sent to instance”。你知道可能出了什么问题吗?我从谷歌获取步骤列表,我的数组中有 47 个解码点,但是这个 unarchiveObjectWithData 调用使应用程序崩溃。如果您知道原因,请告诉我。再次感谢您的解决方案。问候,乔治
    • @George 可能存在取消归档,因为我必须更早地归档数据。如果您直接使用,则不需要。
    猜你喜欢
    • 2012-08-13
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 2011-08-31
    相关资源
    最近更新 更多