2014-09-13 1 views
-1

iOS 용 Google Maps API를 구현했습니다. 나는 성공적으로지도를로드하고 그것에 두 개의 핀을 떨어 뜨렸다. 또한 Google Maps Directions API를 구현했으며 Json 응답을 받았습니다. 지도를 디코딩하고 길 찾기를 표시하는 방법을 모르겠습니다. 내가 다음에 무엇을 알려 주시기 바랍니다JSON 정보보기

direction.h

-(void)retrieveDirectionsFromOrigin:(CLLocationCoordinate2D)origin toDestination:(CLLocationCoordinate2D)destination 
{ 
    NSString *directionsURL=[NSString    stringWithFormat:@"http://maps.google.com/maps/api/directions/json? origin=%f,%f&destination=%f,%f&sensor=false",origin.latitude,origin.longitude,destination.longitude,destination.longitude]; 
    _directionsURL=[NSURL URLWithString:directionsURL]; 
    [self retrieveDirections:nil withDelegate:self]; 
} 

-(void)retrieveDirections:(SEL)selector withDelegate:(id)delegate{ 
dispatch_async(dispatch_get_main_queue(), ^{ 
    NSData *data = [NSData dataWithContentsOfURL:_directionsURL]; 
    [self fetchedData:data withDelegate:delegate]; 
}); 
} 

-(void)fetchedData:(NSData *)data withDelegate:(id)delegate{ 
NSError* error; 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
NSLog(@"Directions %@",json); 
} 

에서 MapViewController.h

-(void)viewDidLoad 
{ 

// Creates a marker in the center of the map. 
GMSMarker *marker = [[GMSMarker alloc] init]; 
GMSMarker *marker2 = [[GMSMarker alloc]init]; 
marker.position = CLLocationCoordinate2DMake(21.422492, 39.826169); 
marker2.position = CLLocationCoordinate2DMake(21.413333, 39.893333); 
marker.map = mapView; 
marker2.map = mapView; 
directions=[[Directions alloc]init]; 
[directions retrieveDirectionsFromOrigin:marker.position toDestination:marker2.position]; 
[mapViewView setNeedsDisplay]; 
[mapView setNeedsDisplay]; 

} 

에서 : 여기

는 코드입니다.

답변

1

Directions API가 결과를 반환하면 경로 배열을 JSON 경로 배열에 배치합니다. 이 경로는 경유지 지정 여부에 따라 하나 이상의 다리로 구성 될 수 있습니다. 경로 필드 내의 각 행은 다음과 같은 필드를 포함 할 수있다 :

  • 다리 [] 지정된 경로 내 두 위치 사이의
    경로 구간에 대한 정보를 보유하고 어레이를 포함한다. 별도의 다리
    은 지정된 각 경유지 또는 목적지에 표시됩니다. 각 다리 은 일련의 단계로 구성됩니다.

  • overview_polyline에는 결과 방향의 대략적인 경로 인 을 나타내는 인코딩 된 점의 배열이 포함됩니다.

다리 : 다리 어레이의 각 요소는 상기 계산 된 경로의 출발지에서 목적지까지의 여정 단일 레그를 지정 .

  • 는 단계 [상기 여행의 다리 각각 개별 단계에 대한 정보를 표시하는 단계의 배열을 포함한다.

  • start_location은이 다리의 원점 의 위도/경도 좌표를 포함합니다.

  • end_location은이 다리의 지정된 대상의 위도/경도 좌표를 포함합니다.

단계 : 단계 어레이의 각 요소는 상기 계산 된 경로의 한 단계를 정의 . 단계는 방향 경로의 가장 원자 단위이며, 은 여행에서 특정 단일 명령을 설명하는 단일 단계를 포함합니다. 예 : "W. 4th St.에서 좌회전"

다음 단계 :는 지금 당신이있는 NSDictionary에 JSON 변환 한 것으로, 사용하는 각 다리가 긴/위도 얻고 각 다리의 사용자 지정 경로를 DISPALY지도에서 GMSPolygon 객체를 적용 할 위의 키의.