问题描述
我想在地图上绘制与我通过Google Directions API获得的路线JSON相对应的路线:
I want to draw routes on a map corresponding to directions JSON which I am getting through the Google Directions API: http://code.google.com/apis/maps/documentation/directions/
我已经想出如何提取来自阶梯场的纬度和经度,但这并不能很好地遵循弯曲的道路。我认为我需要解码折线信息,我发现Googles有关如何编码折线的说明:
I have figured out how to extract the latitude and longitude from the steps field, however this doesn't follow curvy roads very well. I think what I need is to decode the polyline information, I found Googles instructions on how to encode polylines: http://code.google.com/apis/maps/documentation/utilities/polylinealgorithm.html
我找到了一些Android的代码以及解码折线的Javascript,例如:
I did find some code here for Android and also Javascript on decoding the polylines, for example:
但是对于Objective-C iPhone代码我找不到相同的内容,有人可以帮我这个吗?我敢肯定,如果必须的话,我可以自己做,但如果它已经在某个地方可用,肯定会省时间...
But I can't find same for Objective-C iPhone code, can anybody help me with this? I'm sure I can do it myself if I have to, but it sure would save me some time if it's already available somewhere...
编辑:这里的关键能够逐个字符地解码base64编码。更具体地说,我在谷歌的JSON中得到类似的东西,其中使用base64编码进行编码:
the key here is being able to decode the base64 encoding on a character by character basis. To be more specific, I get something like this in JSON from Google which is encoded using base64 encoding among other things:
... "overview_polyline" : {
"points" : "ydelDz~vpN_@NO@QEKWIYIIO?YCS@WFGBEBICCAE?G@y@RKBEBEBAD?HTpB@LALALCNEJEFSP_@LyDv@aB\\GBMB"
},
...
注意:我应该提到这个问题是指谷歌地图API v1,使用GMSPolyLine polyLineWithPath在v2中更容易做到这一点,因为下面的许多答案都会告诉你(感谢@cdescours)。
Note: I should mention that this question refers to Google Maps API v1, it is much easier to do this in v2 using GMSPolyLine polyLineWithPath as many answers below will tell you (thanks to @cdescours).
推荐答案
我希望链接到我自己的如果它与问题相关,但我过去已经解决了这个问题。链接帖子的独立答案:
I hope it's not against the rules to link to my own blog post if it's relevant to the question, but I've solved this problem in the past. Stand-alone answer from linked post:
@implementation MKPolyline (MKPolyline_EncodedString)
+ (MKPolyline *)polylineWithEncodedString:(NSString *)encodedString {
const char *bytes = [encodedString UTF8String];
NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSUInteger idx = 0;
NSUInteger count = length / 4;
CLLocationCoordinate2D *coords = calloc(count, sizeof(CLLocationCoordinate2D));
NSUInteger coordIdx = 0;
float latitude = 0;
float longitude = 0;
while (idx < length) {
char byte = 0;
int res = 0;
char shift = 0;
do {
byte = bytes[idx++] - 63;
res |= (byte & 0x1F) << shift;
shift += 5;
} while (byte >= 0x20);
float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1));
latitude += deltaLat;
shift = 0;
res = 0;
do {
byte = bytes[idx++] - 0x3F;
res |= (byte & 0x1F) << shift;
shift += 5;
} while (byte >= 0x20);
float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1));
longitude += deltaLon;
float finalLat = latitude * 1E-5;
float finalLon = longitude * 1E-5;
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(finalLat, finalLon);
coords[coordIdx++] = coord;
if (coordIdx == count) {
NSUInteger newCount = count + 10;
coords = realloc(coords, newCount * sizeof(CLLocationCoordinate2D));
count = newCount;
}
}
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coords count:coordIdx];
free(coords);
return polyline;
}
@end
这篇关于如何将Google Directions API折线字段解码为适用于iPhone的objective-C中的lat长点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!