Ios 如何从iphone中的谷歌地图解析谷歌方向api数据?
我使用代码从google direction api解析坐标和多段线:Ios 如何从iphone中的谷歌地图解析谷歌方向api数据?,ios,google-maps,Ios,Google Maps,我使用代码从google direction api解析坐标和多段线: -(void)routeParser:(NSString *)sourceAddress andDestination:(NSString *)destinationAddress{ NSLog(@"des sour %@",sourceAddress); NSError *error = nil; NSString* apiUrlStr = [NSString stringWithFormat:@
-(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);
}
-(void)routeParser:(NSString*)源地址和目标地址:(NSString*)目标地址{
NSLog(@“des sour%@”,源地址);
n错误*错误=nil;
NSString*APIRLSTR=[NSString stringWithFormat:@]http://maps.googleapis.com/maps/api/directions/json?origin=%@&目的地=%@&传感器=false“,源地址,目的地地址];
//&备选方案=正确
NSURL*APIRL=[NSURL URLWithString:APIRLSTR];
NSString*apiResponse=[NSString stringWithContentsOfURL:APIRL编码:NSASCIIStringEncoding错误:&错误];
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”];
对于(int i=0;iGoogle拒绝通过客户端脚本进行访问。您需要处理服务器端以下载json数据并将json结果传递给客户端
更新:
字符串Origin=“印度泰米尔纳德邦钦奈”;
字符串Destination=“印度卡纳塔克邦班加罗尔”
var geojsonurl=”http://maps.googleapis.com/maps/api/directions/json?origin=“+Origin+”&destination=“+destination+”&sensor=true”;
WebClient c=新的WebClient();
var json=c.DownloadString(geojsonurl);
GoogleMapsRoutes data=JsonConvert.DeserializeObject(json);
StringBuilder Originstrlatlng=新StringBuilder();
StringBuilder deststrlatng=新StringBuilder();
如果(!(数据==null))
{
foreach(数据中的变量项。路由[0]。支腿[0]。步骤)
{
Originstrlatlng.Append(item.start_location.lat.ToString()+”,“+item.start_location.lng.ToString()+”);
deststrlatng.Append(item.end_location.lat.ToString()+”,“+item.end_location.lng.ToString()+”;
}
}
公共类GoogleMapsRoutes
{
公共列表路由{get;set;}
公共字符串状态{get;set;}
}
公共课东北
{
公共双lat{get;set;}
公共双精度{get;set;}
}
西南公务舱
{
公共双lat{get;set;}
公共双精度{get;set;}
}
公共类界限
{
公共东北{get;set;}
公共西南西南{get;set;}
}
公共类距离
{
公共字符串文本{get;set;}
公共int值{get;set;}
}
公共课时长
{
公共字符串文本{get;set;}
公共int值{get;set;}
}
公共类结束位置
{
公共双lat{get;set;}
公共双精度{get;set;}
}
公开课
{
公共双lat{get;set;}
公共双精度{get;set;}
}
公共类距离2
{
公共字符串文本{get;set;}
公共int值{get;set;}
}
公共课持续时间2
{
公共字符串文本{get;set;}
公共int值{get;set;}
}
公共类结束位置2
{
公共双lat{get;set;}
公共双精度{get;set;}
}
公共类多段线
{
公共字符串点{get;set;}
}
公务舱2
{
公共双lat{get;set;}
公共双精度{get;set;}
}
公共类步骤
{
公共距离2距离{get;set;}
公共持续时间2持续时间{get;set;}
public EndLocation2 end_位置{get;set;}
公共字符串html_指令{get;set;}
公共多段线多段线{get;set;}
公共位置2开始位置{get;set;}
公共字符串移动模式{get;set;}
公共字符串{get;set;}
}
公务舱航段
{
公共距离距离{get;set;}
公共持续时间{get;set;}
公共字符串结束地址{get;set;}
公共结束位置结束位置{get;set;}
公共字符串起始地址{get;set;}
公共位置开始位置{get;set;}
公共列表步骤{get;set;}
通过_航路点{get;set;}的公共列表
}
公共类概览多段线
{
公共字符串点{get;set;}
}
公务舱路线
{
公共边界{get;set;}
公共字符串版权{get;set;}
公共列表{get;set;}
公共概览多段线概述\u多段线{get;set;}
公共字符串摘要{get;set;}
公共列表警告{get;set;}
公共列表航路点\u顺序{get;set;}
}
这很有效。只要试试。-(无效)getRoute
- (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];
}
}
{
polyline.map=nil;
[谷歌地图视图清除];
NSString*strUrl=[NSString stringWithFormat:@]https://maps.googleapis.com/maps/api/directions/json?&alternatives=true&origin=%@&destination=%@&key=%@&sensor=false”,self.sourceText.text,self.destnext.text,googleAPI_key];
[自路由TAURL1:strUrl];
[self.view addSubview:self.getInfo];
[self.view addSubview:customIndicatorView];
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
if(webData)//webData是NSData的对象
{
[self-gettingData:webData];//调用方法gettingData
}
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
webData=nil;
}
//之后,使用gettingData方法
-(void)获取数据:(NSData*)数据
{
NSDictionary*allDataDictionary=[NSJSONSerialization
JSONObjectWithData:数据选项:0
错误:无];
如果([[NSString stringWithFormat:@“%@”,[allDataDictionary objectForKey
:@“状态”]]isEqualToString:@“确定”])
{
collectionArray=[[NSMutableArray alloc]init];
NSMutableArray*legsArray=[[NSMutableArray alloc]initWithArray:allDataDictionary[@“routes”]];
对于(int i=0;i<[legsArray计数];i++)
- (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];
}
}