iOS GoogleMap 路徑規(guī)劃 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=起點(diǎn)經(jīng)緯度 destination=終點(diǎn)經(jīng)緯度
然后用請(qǐng)求一下:

  • (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就是得到的數(shù)據(jù)
    });
    }];
    [task resume];
    }
    //解析json,拿到步行時(shí)間

  • (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]];//不行時(shí)間
    
          
      }else{
         
    
      }
    

    }else{
    }
    }
    按照以上步驟就能拿到步行時(shí)間,但是你有可能會(huì)遇到這個(gè)問(wèn)題
    You have exceeded your daily request quota for this API. We recommend registering for a key at the Google Developers Console
    意思是說(shuō)你每日請(qǐng)求這個(gè)api的數(shù)量已經(jīng)超了,希望你可以去google后臺(tái)申請(qǐng)一個(gè)key。此時(shí)我把谷歌服務(wù)的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
    然后再次請(qǐng)求會(huì)報(bào)如下錯(cuò)誤:
    This IP, site or mobile application is not authorized to use this API key. How to fix this issue for an ios app?
    原因是我們谷歌服務(wù)的key和directions的key不一樣的,所以不能亂用,糾結(jié)了好久最后在這里找到答案
    https://stackoverflow.com/questions/26195282/google-directions-api-failing-to-return-results-in-ios?noredirect=1&lq=1
    具體操作是:
    進(jìn)入這個(gè)網(wǎng)址http://developers.google.com/maps/documentation/directions
    然后點(diǎn)擊上邊的獲取密鑰

    圖片.png

    圖片.png

    然后會(huì)有一個(gè)key,復(fù)制替換上邊的key就行了。
    如果遇到請(qǐng)求失敗,錯(cuò)誤是-1002,是因?yàn)檫@個(gè)key中有特殊字符,例如--等,把url字符串uft8編碼之后再請(qǐng)求,如下:
    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];

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容