Objective-C:如何使用Google API在两点之间进行路由

时间:2015-12-22 10:50:23

标签: ios objective-c google-maps mkmapview mkpolyline

我正在开发一个应用程序,帮助用户知道到达目的地的路线。 API为:http://maps.googleapis.com/maps/api/directions/json?origin=11.02084503,76.97969465&destination=11.009534,76.989613&sensor=false,我收到以下回复。

{
   "geocoded_waypoints" : [
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJrXZekU5YqDsReHrFIWnMGok",
         "types" : [ "route" ]
      },
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJF12UgMlZqDsROtDisRksGJA",
         "types" : [ "route" ]
      }
   ],
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 11.0208452,
               "lng" : 76.9911298
            },
            "southwest" : {
               "lat" : 11.0074033,
               "lng" : 76.9796841
            }
         },
         "copyrights" : "Map data ©2015 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "2.7 km",
                  "value" : 2660
               },
               "duration" : {
                  "text" : "8 mins",
                  "value" : 488
               },
               "end_address" : "PN Palayam Rd, Pudur, Coimbatore, Tamil Nadu 641045, India",
               "end_location" : {
                  "lat" : 11.0097234,
                  "lng" : 76.9897812
               },
               "start_address" : "NGR St, G K D Nagar, P N Palayam, Coimbatore, Tamil Nadu 641044, India",
               "start_location" : {
                  "lat" : 11.0208452,
                  "lng" : 76.9796841
               },
               "steps" : [
                  {
                     "distance" : {
                        "text" : "0.4 km",
                        "value" : 403
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 73
                     },
                     "end_location" : {
                        "lat" : 11.0172273,
                        "lng" : 76.9798629
                     },
                     "html_instructions" : "Head \u003cb\u003esouth\u003c/b\u003e on \u003cb\u003eNGR St\u003c/b\u003e toward \u003cb\u003eJaganatha Perumal Koil Rd\u003c/b\u003e",
                     "polyline" : {
                        "points" : "iogbA_bztMR?f@?rE?~C?zAKD?tBMjAI"
                     },
                     "start_location" : {
                        "lat" : 11.0208452,
                        "lng" : 76.9796841
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.4 km",
                        "value" : 374
                     },
                     "duration" : {
                        "text" : "2 mins",
                        "value" : 101
                     },
                     "end_location" : {
                        "lat" : 11.0140525,
                        "lng" : 76.98077259999999
                     },
                     "html_instructions" : "Continue onto \u003cb\u003eNetaji Rd\u003c/b\u003e",
                     "polyline" : {
                        "points" : "uxfbAccztMLENELEJCFCDCDE@?BE@C@ABCBADCHENEr@WZId@MFCFCFAFAH?H?L?H?N?R?B?P?R?D?NAVCr@IFAn@IVEz@Q"
                     },
                     "start_location" : {
                        "lat" : 11.0172273,
                        "lng" : 76.9798629
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.7 km",
                        "value" : 709
                     },
                     "duration" : {
                        "text" : "2 mins",
                        "value" : 107
                     },
                     "end_location" : {
                        "lat" : 11.0129988,
                        "lng" : 76.98606699999999
                     },
                     "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e after UNION BANK ATM (on the left)\u003cdiv style=\"font-size:0.9em\"\u003eContinue to follow Perundurai-Coimbatore Rd\u003c/div\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by CITY UNION BANK AND ATM (on the left)\u003c/div\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "ydfbAyhztMEi@Iu@WgBW_BKq@Km@Ms@ESMi@Q_@Yq@ISEMIYESASAO?ABODOLUNQNQJIFIj@m@NOTQFGx@e@l@[j@]d@YZO^Q"
                     },
                     "start_location" : {
                        "lat" : 11.0140525,
                        "lng" : 76.98077259999999
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.9 km",
                        "value" : 873
                     },
                     "duration" : {
                        "text" : "2 mins",
                        "value" : 132
                     },
                     "end_location" : {
                        "lat" : 11.0074361,
                        "lng" : 76.9911298
                     },
                     "html_instructions" : "At Axis Bank, continue onto \u003cb\u003ePuliakulam Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Times Of India (on the right in 650 m)\u003c/div\u003e",
                     "polyline" : {
                        "points" : "g~ebA}i{tMNKnAy@pBwA|BkBxBcBHG`Ao@POLMLOJSFUF[@c@?]A[?U?S?MDQ?ADKDIFIFIFGHILIVOn@YvB_AtB}@\\M`Bs@"
                     },
                     "start_location" : {
                        "lat" : 11.0129988,
                        "lng" : 76.98606699999999
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.3 km",
                        "value" : 301
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 75
                     },
                     "end_location" : {
                        "lat" : 11.0097234,
                        "lng" : 76.9897812
                     },
                     "html_instructions" : "Make a \u003cb\u003eU-turn\u003c/b\u003e at \u003cb\u003eStreet Number 34\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Vidhya Niketan Matric Hr Sec School (on the right)\u003c/div\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the left\u003c/div\u003e",
                     "maneuver" : "uturn-right",
                     "polyline" : {
                        "points" : "o{dbAqi|tMFHcBr@]NaCbACBoAh@e@TWLSHMHMHGHA@"
                     },
                     "start_location" : {
                        "lat" : 11.0074361,
                        "lng" : 76.9911298
                     },
                     "travel_mode" : "DRIVING"
                  }
               ],
               "via_waypoint" : []
            }
         ],
         "overview_polyline" : {
            "points" : "iogbA_bztMnL?bHc@v@UTMJOb@Q|Bs@`@GnB?`BOjCc@O_Bo@gEk@gDMi@Q_@c@eAOg@Gg@AQH_@\\g@Z[xAyAzCgB`Ai@n@]`EqCvFoEjAw@^]Xc@Nq@@aAAq@?a@DSJUNSPQd@YzHeD`Bs@FHaCbA{FfCgAj@IJ"
         },
         "summary" : "Perundurai-Coimbatore Rd and Puliakulam Rd",
         "warnings" : [],
         "waypoint_order" : []
      }
   ],
   "status" : "OK"
}

现在我想在MKMapView中绘制我的目的地到目的地之间的路线。

1 个答案:

答案 0 :(得分:0)

获得api回复后使用此功能。

   GMSPath *path =[GMSPath pathFromEncodedPath:resultDict[@"routes"][0][@"overview_polyline"][@"points"]];
         self.polyline = [GMSPolyline polylineWithPath:path];
         self.polyline.strokeWidth = 4;
         self.polyline.strokeColor = [UIColor colorWithRed:0.0/255.0 green:179.0/255.0 blue:253.0/255.0 alpha:1];
         self.polyline.map = self.googleMap;

 -(void)showRoute:(MKDirectionsResponse *)response
{
   for (MKRoute *route in response.routes)
   {
     [routeMap
     addOverlay:route.polyline level:MKOverlayLevelAboveRoads];

     for (MKRouteStep *step in route.steps)
     {
        NSLog(@"%@", step.instructions);
      }
    }
 }


- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
 {
   [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    MKPolylineRenderer *renderer =
    [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    renderer.strokeColor = [UIColor blueColor];
    renderer.lineWidth = 5.0;
    return renderer;
 }