iOS GoogleMap 路径规划 Directions

谷歌给的api:
https://maps.googleapis.com/maps/api/directions/json?origin=39.99,116.31&destination=39.99,116.41&sensor=false&mode=walking

origin=起点经纬度 destination=终点经纬度
然后用请求一下:

  • (void)getLine{
    NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false&mode=walking&language=en",_coor.latitude,_coor.longitude,_lat,_lng];
    NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (!data) {
    return ;
    }
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error) {
    return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
    [self configWithData:json];//json就是得到的数据
    });
    }];
    [task resume];
    }
    //解析json,拿到步行时间

  • (void)configWithData:(NSDictionary *)data{
    if (data) {
    NSArray *arrRouts=[data objectForKey:@"routes"];

      GMSPolyline *polyline = nil;
      if ([arrRouts count] > 0)
      {
          NSDictionary *routeDict = [arrRouts objectAtIndex:0];
          NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"];
          NSString *points = [routeOverviewPolyline objectForKey:@"points"];
          GMSPath *path = [GMSPath pathFromEncodedPath:points];
          polyline = [GMSPolyline polylineWithPath:path];
          polyline.strokeWidth = 4.f;
          polyline.strokeColor =  MainColor1;
          polyline.map = self.googleMapView;
          
          GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];
          GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds];
          [self.googleMapView moveCamera:update];
          NSArray *routeArr = [data objectForKey:@"routes"];
          NSDictionary *routeDic = [NSDictionary dictionaryWithDictionary:[routeArr objectAtIndex:0]];
          NSString *time = [NSString stringWithFormat:@"%@",[[[[[routeDic objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"duration"] objectForKey:@"text"] description]];//不行时间
    
          
      }else{
         
    
      }
    

    }else{
    }
    }
    按照以上步骤就能拿到步行时间,但是你有可能会遇到这个问题
    You have exceeded your daily request quota for this API. We recommend registering for a key at the Google Developers Console
    意思是说你每日请求这个api的数量已经超了,希望你可以去google后台申请一个key。此时我把谷歌服务的key拼接上如下:
    https://maps.googleapis.com/maps/api/directions/json?origin=39.99,116.31&destination=39.99,116.41&sensor=false&mode=walking&key=AIXXDDDDDDD
    然后再次请求会报如下错误:
    This IP, site or mobile application is not authorized to use this API key. How to fix this issue for an ios app?
    原因是我们谷歌服务的key和directions的key不一样的,所以不能乱用,纠结了好久最后在这里找到答案
    https://stackoverflow.com/questions/26195282/google-directions-api-failing-to-return-results-in-ios?noredirect=1&lq=1
    具体操作是:
    进入这个网址http://developers.google.com/maps/documentation/directions
    然后点击上边的获取密钥

    图片.png

    图片.png

    然后会有一个key,复制替换上边的key就行了。
    如果遇到请求失败,错误是-1002,是因为这个key中有特殊字符,例如--等,把url字符串uft8编码之后再请求,如下:
    NSString *urlStr = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false&mode=walking&language=en&key=AIzaXXXXXlwsvkCHQf--9xIKU0_zyMJ6To ",_coor.latitude,_coor.longitude,_lat,_lng];
    NSString *url = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

你可能感兴趣的:(iOS GoogleMap 路径规划 Directions)