【问题标题】:Display Direction using google api in ios get在 ios get 中使用 google api 显示方向
【发布时间】:2015-10-21 06:53:07
【问题描述】:

在下面的代码运行中,我得到了来自 url 的响应,但是当我尝试获取 encodedPoints 时,它给了我一个空值。我也更新了RegexKitLite,但很可能。不解决。欢迎任何建议谢谢您提前。

 NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];
            NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude];
            NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", saddr, daddr];
    //      http://maps.googleapis.com/maps/api/directions/json?origin=41.029598,28.972985&destination=41.033586,28.984546&sensor=false%EF%BB%BF%EF%BB%BF
            NSURL *apiUrl = [NSURL URLWithString:[apiUrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            NSLog(@"api url: %@", apiUrl);
           NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:nil error:nil];
            NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];
            NSLog(@"encodedPoints: %@", encodedPoints);
            if (encodedPoints) {
                return [self decodePolyLine:[encodedPoints mutableCopy]];
            }
            else {
                return NO;
            }

【问题讨论】:

    标签: ios google-maps-api-3


    【解决方案1】:

    我认为同步进行 API 请求不是一个好方法,尤其是当用户的手机网络连接不佳时,它会减慢您的应用程序的响应速度。因此,您应该使用NSURLSession 进行异步 API 请求。

    此外,Directions API 可能会为您的请求返回多个路线。所以最好使用NSArray 来存储你的折线点。

    示例代码:

    - (void)getPolyline {
        NSURL *url = [[NSURL alloc] initWithString:@"https://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&key=YOUR_API_KEY"];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
        NSURLSession *session = [NSURLSession sharedSession];
    
        [[session dataTaskWithRequest:request completionHandler:
          ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
              if (!error) {
                  NSError *jsonError;
                  NSDictionary *dict = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:data options:nil error:&jsonError];
                  if (!jsonError) {
    
                      NSArray *routesArray = (NSArray*)dict[@"routes"];
    
                      NSMutableArray *points = [NSMutableArray array];
    
                      for (NSDictionary *route in routesArray) {
                          NSDictionary *overviewPolyline = route[@"overview_polyline"];
                          [points addObject:overviewPolyline[@"points"]];
                      }
    
                      NSLog(@"%@", points);
    
                  }
              } else {
                  //print error message
                  NSLog(@"%@", [error localizedDescription]);
              }
          }] resume];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      相关资源
      最近更新 更多