Is there a way to get directions(just routes) using google for mkmapview?

前端 未结 3 1037
既然无缘
既然无缘 2021-01-15 06:53

I\'m using swift on iOS and using MKMapView. I\'ve been working on giving a user a from - to textfield and letting the user have a form of route between the from and to loca

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 07:31

    Using google api to find directions between origin and destination.

    Required parameters:

    • origin : lat + long current location or place ID 
.

    • destination : lat + long destination location or place ID 
.

    • Key: to create new google api key .

    Optional parameters:

    • mode (defaults to driving) driving give you more options, or set
      walking, bicycling, transit.
    • alternatives : false give u just one route, option true give all directions between origin and destinations, then you can choose which route u want deponent on duration, or distance inside legs.

    then convert points (inside overview_polyline) to list of CLLocationCoordinate2D using Polyline library https://github.com/raphaelmor/Polyline

    Like this:

    func convertPointToCoordinates() -> [CLLocationCoordinate2D]? {

    let polyline = Polyline(encodedPolyline: overviewPolyline.points ?? "")

    return polyline.coordinates

    }

    let coordinates = convertPointToCoordinates()

    let polyLine = MKPolyline(coordinates: coordinates ?? [], count: (coordinates?.count ?? 0))

    • to add polyline on map view:

      self.mapView.add(polyLine)

    • to focus on polyline region:

      self.mapView.setVisibleMapRect(polyLine.boundingMapRect, edgePadding: UIEdgeInsets.init(top: 60, left: 60, bottom: 60, right: 60), animated: true)

提交回复
热议问题