我编写了简单的应用程序,它在MapKit上的两个位置之间绘制路径。我正在使用Google Map API。我使用了我在网上找到的资源,这里是我用来向Google提出请求的代码:
_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];
这完美无瑕。当我跑步并试图显示洛杉矶和芝加哥之间的路线时,这是它的样子:
BUT。当我将地图缩放到街道级别时,这是路线的样子:
有人知道如何在地图缩放时实现我正在绘制的路线吗?我想路线显示街道的确切路径。我不知道是否需要在我的Google请求中添加一些其他参数。
任何帮助或建议都会很棒。非常感谢提前!
[编辑#1:添加Google的请求网址和回复]
从上面的代码创建操作对象后,我的请求URL如下所示:
http://maps.googleapis.com/maps/api/directions/json?sensor=true&destination=34%2E052360,-118%2E243560&origin=41%2E903630,-87%2E629790
只需将该网址粘贴到您的浏览器中,您就会看到Google发送的JSON数据作为我在代码中获得的响应。
[编辑#2:解析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;
}
答案 0 :(得分:1)
看起来谷歌没有给你所有的积分,或者你没有看到所有的观点。实际上,我希望地标之间有折线,而不仅仅是像你一样的地标(有一条直线)。
我不太确定他们使用与Google使用的墨卡托投影截然不同的投影。
答案 1 :(得分:0)
我认为MapKit使用的投影与Google地图使用的投影不同。 MapKit uses Cylindrical Mercator,Google uses a variant of the Mercator Projection。
在坐标系之间转换 虽然你通常指定 使用纬度和经度值的地图上的点,可能有 需要与其他坐标系进行转换的时间。 例如,您通常在指定形状时使用地图点 叠加层。
引自Apple: