ios - iPhone map 路线问题

标签 ios objective-c iphone

我正在尝试按照以下代码获取Google Maps API响应

NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];

NSLog(@"api url: %@", apiUrl);

NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSStringEncodingConversionAllowLossy error:Nil];

我从谷歌API得到了这个apiResponse json数据:
   {
 "routes" : [
  {
     "bounds" : {
        "northeast" : {
           "lat" : 23.0236292,
           "lng" : 72.60381579999999
        },
        "southwest" : {
           "lat" : 22.990296,
           "lng" : 72.5293059
        }
     },
     "copyrights" : "Map data ©2015 Google",
     "legs" : [
        {
           "distance" : {
              "text" : "10.8 km",
              "value" : 10797
           },
           "duration" : {
              "text" : "23 mins",
              "value" : 1403
           },
           "end_address" : "35, Shivranjani, Jodhpur, Ahmedabad, Gujarat 380015, India",
           "end_location" : {
              "lat" : 23.0234672,
              "lng" : 72.5293059
           },
           "start_address" : "6, Natvarlal Raval Marg, Archana Society, Bhairavnath, Maninagar, Ahmedabad, Gujarat 380008, India",
           "start_location" : {
              "lat" : 22.990296,
              "lng" : 72.60381579999999
           },
           "steps" : [
              {
                 "distance" : {
                    "text" : "0.9 km",
                    "value" : 898
                 },
                 "duration" : {
                    "text" : "2 mins",
                    "value" : 123
                 },
                 "end_location" : {
                    "lat" : 22.9943614,
                    "lng" : 72.5962808
                 },
                 "html_instructions" : "Head \u003cb\u003enorthwest\u003c/b\u003e on \u003cb\u003eNatvarlal Raval Marg\u003c/b\u003e toward \u003cb\u003eNatvarlal Raval Marg\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Nelsons International School (on the right)\u003c/div\u003e",
                 "polyline" : {
                    "points" : "khikC{lczLMBu@jBSd@}AtDg@hA_@z@o@rAYl@O\\yArCgA|BMXQ`@q@bBm@zAcAjCSf@kArCMZ"
                 },
                 "start_location" : {
                    "lat" : 22.990296,
                    "lng" : 72.60381579999999
                 },
                 "travel_mode" : "DRIVING"
              },
              {
                 "distance" : {
                    "text" : "0.2 km",
                    "value" : 192
                 },
                 "duration" : {
                    "text" : "1 min",
                    "value" : 40
                 },

我正在使用以下代码来获取多边形线点
   NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"points\":\\\"([^\\\"]*)\\\"" options:0 error:NULL];

   NSTextCheckingResult *match = [regex firstMatchInString:apiResponse options:0 range:NSMakeRange(0, [apiResponse length])];

   NSString *encodedPoints = [apiResponse substringWithRange:[match rangeAtIndex:1]];
   return [self decodePolyLine:[encodedPoints mutableCopy]];

但它返回@“”编码点

正则表达式有什么问题吗?

最佳答案

当您在字典中得到响应时,请执行此操作,

        NSMutableArray *pathArray = [NSMutableArray new];
        NSArray *routes = [[directionResponse directionResponse] objectForKey:@"routes"];
        NSDictionary *route = [routes lastObject];
        DLog(@"Array routes :%@",routes);

        if (route) {
            NSString *overviewPolyline = [[route objectForKey: @"overview_polyline"] objectForKey:@"points"];
            pathArray = [self decodePolyLine:overviewPolyline];
        }
        NSInteger numberOfSteps = pathArray.count;

        CLLocationCoordinate2D coordinates[numberOfSteps];
        for (NSInteger index = 0; index < numberOfSteps; index++)
        {
            CLLocation *location = [pathArray objectAtIndex:index];
            CLLocationCoordinate2D coordinate = location.coordinate;
            coordinates[index] = coordinate;
        }
        MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];

关于ios - iPhone map 路线问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31071181/

相关文章:

ios - 在 swift4 中移动 View 时屏幕是黑色的

iphone - iOS 为 UIImageView 获取图像的可见部分

objective-c - 外部自动释放池

ios - applicationWillEnterForeground 与 applicationDidBecomeActive、applicationWillResignActive 与 applicationDidEnterBackground

iphone - 将一组新数据写入 Plist 而不是覆盖它

iOS 7 UITableView 默认分隔符在重新排序后变得奇怪

iphone - 使用 NSUserDefaults 持久化 MPMediaItemCollection 对象

iphone - UILabel 的 sizeThatFits 是否返回它的高度?

iphone - XCode 4 中的 iOS 跟踪?

iphone - 从多个表中过滤数据