Tôi đã viết ứng dụng đơn giản vẽ đường giữa hai vị trí trên MapKit. Tôi đang sử dụng API Google Map. Tôi đã sử dụng các tài nguyên tôi tìm thấy trực tuyến và đây là mã tôi đang sử dụng để gửi yêu cầu tới Google:MapKit - Thực hiện tuyến đường theo các đường phố khi bản đồ được phóng to
_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];
Điều này hoạt động hoàn hảo. Khi tôi chạy này và cố gắng hiển thị tuyến đường giữa LA và Chicago, dưới đây là cách nó trông giống như:
NHƯNG. Khi tôi phóng to bản đồ đến cấp đường phố, dưới đây là cách tuyến đường trông giống như:
Có ai biết làm thế nào tôi có thể đạt được điều đó đường Tôi vẽ sau đường phố khi bản đồ được thu nhỏ? Tôi muốn lộ trình hiển thị con đường chính xác qua các đường phố. Tôi không biết liệu có cần thêm một số thông số nào đó vào yêu cầu của tôi cho Google hay không.
Bất kỳ trợ giúp hoặc lời khuyên nào đều tuyệt vời. Rất cám ơn trước!
[sửa # 1: Thêm URL request và response từ Google]
URL yêu cầu của tôi sau khi tạo đối tượng hoạt động từ mã trên trông như thế này:
http://maps.googleapis.com/maps/api/directions/json?sensor=true&destination=34%2E052360,-118%2E243560&origin=41%2E903630,-87%2E629790
Chỉ cần dán mà URL đến trình duyệt của bạn và bạn sẽ thấy dữ liệu JSON mà Google gửi dưới dạng phản hồi mà tôi cũng nhận được trong mã của mình.
[sửa # 2: Phân tích câu trả lời từ Google và xây dựng con đường]
- (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;
}
tôi đang sử dụng thư viện này. điều này làm việc tốt cho tôi. https://github.com/kadirpekel/MapWithRoutes –