iphone地图路线问题

时间:2015-06-26 10:44:38

标签: ios objective-c iphone

我正在尝试使用代码来获取谷歌地图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];

我从google 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]];

但它会返回@"" for encodedPoints

正则表达式有问题吗?

2 个答案:

答案 0 :(得分:2)

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

        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];

答案 1 :(得分:1)

我不认为你必须使用正则表达式,你可以在客观c中使用一个简单的json解析器并访问poyline及其点,参考: - http://pivotallabs.com/parsing-json-in-objective-c/