2011-10-31 5 views
0

특정 주행 거리를 얻을 수있는 방법이 있습니까 geo-cordinate? 나는 http://maps.googleapis.com/maps/api/directions/output?parameters을 사용하고 결과를 얻기 위해 JSON 출력을 얻어야한다는 것을 알고 있습니다.특정 좌표로의 주행 거리 계산

이 작업을 수행하는 방법을 설명하는 자습서가 있습니까? 또는 내가 시작하는 샘플 코드는 무엇입니까?

+1

이 튜토리얼에 대해 잘 모르기 때문에 Google을 추천 해 드리겠습니다. – khr055

+0

아래에서 답변을 얻을 수 있습니다. – sharon

답변

4

다음을 시도해보십시오. 이 코드는 Google Directions Services에서 반환 한 모든 방향 (대체 경로도 포함)을 가져옵니다. 당신은 2 단계 (구문 분석 응답)

1 단계에서 거리를 얻을 수 있습니다에서 및 위치

NSMutableString *googleURL = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=true&alternatives=true", searchFrom.text, searchTo.text]]; 
NSURL *url = [NSURL URLWithString:googleURL]; 
[googleURL release]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];   
[NSURLConnection connectionWithRequest:request delegate:self]; 
request = nil; 
responseData = [[NSMutableData alloc] init]; 

2 단계에 입력 : 당신이 만든 쿼리의 응답을받을 때, 그 결과를 분석

- (id)parseResponseWithDictionary:(NSDictionary *)dictResponse { 
    if ([[dictResponse objectForKey:@"status"] isEqualToString:kGoogleDirectionsStatusOK]) { 
     NSArray *listRoutes = (NSArray *)[dictResponse objectForKey:@"routes"]; 
     NSMutableString *result = nil; 
     NSMutableArray *listPolylines = nil; 
     for (NSDictionary *dictRouteValues in listRoutes) { 
      NSDictionary *dictPolyline = [dictRouteValues objectForKey:@"overview_polyline"]; 
      if (!result) { 
       result = [[NSMutableString alloc] init]; 
      } 
      [result appendString:[dictPolyline objectForKey:@"points"]]; 
      if (!listPolylines) { 
       listPolylines = [[NSMutableArray alloc] init]; 
      } 
      [listPolylines addObject:result]; 
      [result release]; 
      result = nil; 
     } 
     return [listPolylines autorelease]; 
    } 
    else { 
     NSString *error = @"No result found. Please check start and end location again!"; 
     return error; 
    } 
    return nil; 
} 

3 단계 : 폴리 라인 목록에서 각 폴리 라인을 디코딩합니다. 이것은 경로 좌표를 제공합니다.

-(NSMutableArray *)decodePolyLine:(NSMutableString *)encoded { 
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\" 
           options:NSLiteralSearch 
            range:NSMakeRange(0, [encoded length])]; 
    NSInteger len = [encoded length]; 
    NSInteger index = 0; 
    NSMutableArray *listCoordinates = [[[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]; 
     NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5]; 
     //CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]]; 
     [listCoordinates addObject:[NSString stringWithFormat:@"%f,%f", [latitude floatValue], [longitude floatValue]]]; 
     //[loc release]; 
     [latitude release]; 
     [longitude release]; 
    } 

    return listCoordinates; 
} 
0

이 최신 tutorial/sample code CoreLocation과 MapKit을 사용하면 나에게 도움이되었습니다.