2011-07-22 44 views
0

我想獲取mapkit中兩點之間的路線! ? 我做了一些谷歌的工作ñ發現這一點:-http://spitzkoff.com/craig/ P = 108查看地圖中兩點之間的路線+ iphone + Mapkit

我用這個來顯示地圖和引腳: -

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.navigationController.navigationBarHidden=NO; 
    [email protected]"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; 
} 

和註釋類來顯示名稱和地址。但我應該如何添加更多以顯示兩點之間的路線。

請幫幫我。

在此先感謝

回答

6

您需要調用谷歌的方向服務。閱讀文檔在

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]; 
} 
+0

嗨,謝謝你的解決方案!它對我很好,有一點細節......它崩潰了:P。在這一行上:CLLocation * location =(CLLocation *)[NSKeyedUnarchiver unarchiveObjectWithData:[routes objectAtIndex:i]]; 。錯誤日誌顯示「[CLLocation bytes]:發送到實例的無法識別的選擇器」。你有什麼想法可能是錯的?我得到谷歌的步驟列表,並有47個解碼點在我的數組,但這unarchiveObjectWithData調用使應用程序崩潰。請讓我知道你是否知道爲什麼。再次感謝您的解決方案。問候,喬治 – George

+0

@喬治的unarchiving是可能存在的,因爲我必須早先存檔數據。如果你直接使用,你不會需要。 –

相關問題