gpt4 book ai didi

ios - 如何在 iphone 中解析来自谷歌地图的谷歌方向 api 数据?

转载 作者:行者123 更新时间:2023-11-28 17:54:11 25 4
gpt4 key购买 nike

我使用代码从 google direction 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 并在开始时返回 null apiResponse。我不明白我的错在哪里,有人帮忙吗?

最佳答案

- (void) getRoute
{
polyline.map = nil;
[googleMapView clear];
NSString *strUrl = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?&alternatives=true&origin=%@&destination=%@&key=%@&sensor=false",self.sourceText.text,self.destnText.text,googleAPI_Key];

[self routeDataUrl1:strUrl];
[self.view addSubview:self.getInfo];
[self.view addSubview:customIndicatorView];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
if (webData) //webData is object of NSData
{
[self gettingData:webData ]; //calling the method gettingData
}

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
webData=nil;
}

//after that gettingData method

- (void) gettingData:(NSData *)data
{

NSDictionary *allDataDictionary = [NSJSONSerialization
JSONObjectWithData:data options:0
error:nil];

if ([[NSString stringWithFormat:@"%@",[allDataDictionary objectForKey
: @"status"]] isEqualToString: @"OK"])

{

collectionArray =[[NSMutableArray alloc]init];
NSMutableArray *legsArray = [[NSMutableArray alloc] initWithArray:allDataDictionary[@"routes"]];


for (int i=0; i< [legsArray count]; i++)
{

[collectionArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:legsArray[i][@"legs"][0][@"distance"][@"text"],@"Distance", legsArray[i][@"legs"][0][@"duration"][@"text"],@"Duration",legsArray [i][@"summary"],@"Address",legsArray[i][@"legs"][0][@"summary"][@"html_instructions"][@"text"],@"HtmlInstructions",legsArray[i][@"overview_polyline"][@"points"],@"OverviewPolyline", nil]];
[polyLineArray addObject:legsArray[i][@"legs"][0][@"steps"]];
NSLog(@"11 == %@",legsArray[i][@"legs"][0][@"steps"]);
}
[self.getInfo reloadData]; //where getInfo is the TableView
}
else
{
NSString *msg=[allDataDictionary objectForKey:@"error_message"];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error !" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

[alert show];
}
}

希望这对你有帮助...

关于ios - 如何在 iphone 中解析来自谷歌地图的谷歌方向 api 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19636792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com