我用代碼來解析從谷歌API方向的座標和折線:如何在iPhone中解析谷歌方向API數據從谷歌地圖?
-(void)routeParser:(NSString *)sourceAddress andDestination:(NSString *)destinationAddress{
NSLog(@"des sour %@",sourceAddress);
NSError *error = nil;
NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", sourceAddress, destinationAddress];
//&alternatives=true
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:&error];
NSLog(@"apiResponse %@",apiResponse);
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:apiResponse];
NSLog(@"apiResponse is : %@",[jsonData objectForKey:@"routes"]);
NSArray *routeArray = [[NSArray alloc]init];
routeArray= [jsonData objectForKey:@"routes"];
for(int i=0;i<[routeArray count];i++)
{
NSDictionary *tempDictionary = [routeArray objectAtIndex:i];
if([tempDictionary objectForKey:@"overview_polyline"]!=nil)
{
NSDictionary *secTempDictionary = [tempDictionary objectForKey:@"overview_polyline"];
if([secTempDictionary objectForKey:@"points"]!=nil)
{
NSString * routePoint =[secTempDictionary objectForKey:@"points"];
[routeSetAry addObject:routePoint];
encodedPoints = [secTempDictionary objectForKey:@"points"];
}
// NSLog(@"secTempDictionary is: %@", secTempDictionary);
}
if([tempDictionary objectForKey:@"legs"]!=nil)
{
NSArray *lagArray = [[NSArray alloc]init];
lagArray= [tempDictionary objectForKey:@"legs"];
for(int i=0;i<[lagArray count];i++)
{
NSDictionary *thirdTempDictionary = [lagArray objectAtIndex:i];
if([thirdTempDictionary objectForKey:@"steps"]!=nil)
{
NSArray *stepsArray = [[NSArray alloc]init];
stepsArray= [thirdTempDictionary objectForKey:@"steps"];
for(int i=0;i<[stepsArray count];i++)
{
NSDictionary *forthTempDictionary = [stepsArray objectAtIndex:i];
if([forthTempDictionary objectForKey:@"html_instructions"]!=nil)
{
NSString * directionStr =[forthTempDictionary objectForKey:@"html_instructions"];
NSRange range;
while ((range = [directionStr rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound){
directionStr=[directionStr stringByReplacingCharactersInRange:range withString:@""];
}
[directionStrAry addObject:directionStr];
}
NSDictionary *fifthTempDictionary = [forthTempDictionary objectForKey:@"polyline"];
if([fifthTempDictionary objectForKey:@"points"]!=nil)
{
NSString * routePoint =[fifthTempDictionary objectForKey:@"points"];
[polylineSetAry addObject:routePoint];
// encodedPoints = [fifthTempDictionary objectForKey:@"points"];
}
NSDictionary *sixthTempDictionary =[forthTempDictionary objectForKey:@"distance"];
if([sixthTempDictionary objectForKey:@"text"]!=nil)
{
NSString * distanceStr =[sixthTempDictionary objectForKey:@"text"];
[distanceStrAry addObject:distanceStr];
// encodedPoints = [fifthTempDictionary objectForKey:@"points"];
}
}
}
}
}
}
NSLog(@"routeSetAry is :%@",routeSetAry);
NSLog(@"polylineSetAry is : %i",polylineSetAry.count);
}
,但問題是它記錄空,並在開始返回null apiResponse。我無法理解我的錯在哪裏,有人幫忙?
請使用最新的谷歌API version.I認爲你正在使用舊的API網址。 – Suresh
在瀏覽器此url返回json,所以我認爲這不是backdated – Reyjohn
任何一個可以幫助PLZ?我卡在這裏:( – Reyjohn