MapKit에서 두 위치 간의 경로를 그리는 간단한 응용 프로그램을 작성했습니다. Google Map API를 사용하고 있습니다. 온라인에서 찾은 자료를 사용했으며 Google에 요청할 때 사용하는 코드는 다음과 같습니다.MapKit - 맵 확대시 도로를 따라가는 경로를 만듭니다.
_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]];
[_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]];
[_httpClient setDefaultHeader:@"Accept" value:@"application/json"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:[NSString stringWithFormat:@"%f,%f", coordinate.latitude, coordinate.longitude] forKey:@"origin"];
[parameters setObject:[NSString stringWithFormat:@"%f,%f", endCoordinate.latitude, endCoordinate.longitude] forKey:@"destination"];
[parameters setObject:@"true" forKey:@"sensor"];
NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
NSInteger statusCode = operation.response.statusCode;
if (statusCode == 200)
{
NSLog(@"Success: %@", operation.responseString);
}
else
{
NSLog(@"Status code = %d", statusCode);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", operation.responseString);
}
];
[_httpClient enqueueHTTPRequestOperation:operation];
이것은 완벽하게 작동합니다.
하지만 : 나는 이것을 실행하고 LA와 시카고 사이의 경로를 표시하려고 할 때 그것이 어떻게 보이는지, 여기. 나는 거리 수준에지도를 확대하면, 여기에 경로가 보이는 방법은 다음과 같습니다
사람이지도를 확대하면 내가 그리기하고 그 경로가 거리를 다음과 달성 할 수있는 방법을 알고 있나요? 거리를 통해 정확한 경로를 표시하는 경로를 원합니다. 일부 추가 매개 변수를 Google에 대한 요청에 추가해야하는지 여부는 알 수 없습니다.
도움이나 조언이 도움이 될 것입니다. 미리 많은 감사드립니다!
[편집 # 1 : 구글에서 요청 URL 및 응답 추가]가
위의 코드에서 작업 객체를 생성 한 후 내 요청 URL은 다음과 같습니다를 :
http://maps.googleapis.com/maps/api/directions/json?sensor=true&destination=34%2E052360,-118%2E243560&origin=41%2E903630,-87%2E629790
그냥 붙여 넣기 브라우저 URL을 입력하면 JSON 데이터가 Google 코드로 전송되는 응답으로 전송됩니다.
: 나는 MapKit에서 사용하는 투사 Google지도에서 사용하는 것과 다른 것을 생각
- (void)parseResponse:(NSData *)response
{
NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
NSArray *routes = [dictResponse objectForKey:@"routes"];
NSDictionary *route = [routes lastObject];
if (route)
{
NSString *overviewPolyline = [[route objectForKey: @"overview_polyline"] objectForKey:@"points"];
_path = [self decodePolyLine:overviewPolyline];
}
}
- (NSMutableArray *)decodePolyLine:(NSString *)encodedStr
{
NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];
[encoded appendString:encodedStr];
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[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 *location = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
[array addObject:location];
}
return array;
}
이 라이브러리를 사용하고 있습니다. 이것은 나를 위해 잘 작동하고 있습니다. https://github.com/kadirpekel/MapWithRoutes –