2012-06-21 43 views
1

我拼命試圖找到一種方式來獲取從谷歌API的所有座標(或折線)的數組,以便我可以在MKMapView上繪製我的路線。 (順便說一下,我不想使用RegexKitLite)從谷歌API解碼路線

正如我在其他地方發現的,這應該是調用API的正確方法。

NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr]; 
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr]; 

encodedString = ..... ???? // here i need some code before i can call the following Method 


[self polylineWithEncodedString: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; 

}

回答

0

呼喚......

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@,%@&destination=%f,%f&mode=driving&sensor=false",smAppDelegate.currPosition.latitude, smAppDelegate.currPosition.longitude, ((LocationItem*)selectedAnno).coordinate.latitude, ((LocationItem*)selectedAnno).coordinate.longitude]]]; 

[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

響應...

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
[self.responseDirectionData setLength:0]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
[self.responseDirectionData appendData:data]; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
[connection release]; 
self.responseDirectionData =nil; 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
NSLog(@"connection established"); 
[connection release]; 
NSError *error = nil; 
NSString *responseString=[[NSString alloc]initWithData:responseDirectionData encoding:NSUTF8StringEncoding]; 
SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; 

NSDictionary *jsonObjects = [jsonParser objectWithString:responseString error:&error]; 
NSLog(@"responce = %@",jsonObjects); 

NSMutableArray *ad = [jsonObjects objectForKey:@"routes"]; 
NSMutableArray *data2 = [[ad objectAtIndex:0] objectForKey:@"legs"]; 
NSMutableArray *data3 = [[data2 objectAtIndex:0] objectForKey:@"steps"]; 
NSLog(@"Data3 %@", data3); 

NSMutableArray *polyLinesArray = [[NSMutableArray alloc] init]; 

for (int i = 0; i < [data3 count]; i++) 
{ 
    NSString* encodedPoints = [[[data3 objectAtIndex:i] objectForKey:@"polyline"] valueForKey:@"points"]; 
    MKPolyline *route = [self polylineWithEncodedString:encodedPoints]; 
    [polyLinesArray addObject:route]; 
} 

[self.mapView addOverlays:polyLinesArray]; 
[polyLinesArray release]; 

} 
1

從這裏代碼工作得很好: https://gist.github.com/Muximize/3770235

+ (MKPolyline *)polylineWithGMEncodedString:(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; 
}