2016-10-07 77 views
0

嗨我不知道是否有方法來繪製谷歌地圖iOS中的兩個或更多標記之間的航點。我不想畫直線......但只使用公共道路。這裏是我的一些代碼繪製直線,但它不是我正在尋找的。SWIFT:谷歌地圖繪製航點折線

@objc private func makeGpsPath(){ 
    for i in 0 ..< trailArr.count { 
     path.add(trailArr[i]) 
    } 
    let polyline = GMSPolyline(path: path) 
    polyline.strokeWidth = 5.0 
    polyline.strokeColor = UIColor.black 
    polyline.map = mapViewContainer 
} 

回答

1

要繪製點之間的折線2個礦多,你應該使用谷歌地圖請求閱讀我的情況,我沒有

func drawRoute() { 
    ServerCommunicator.getDotsToDrawRoute(positions: positions, completion: { path in 
     self.route.countRouteDistance(p: path) 
     self.polyline.path = path 
     self.polyline.strokeColor = UserSession.tintColor 
     self.polyline.strokeWidth = 4.0 
     self.polyline.map = self._mapView 
    }) 
} 

此鏈接https://developers.google.com/maps/documentation/directions/intro#Waypoints 和部分與要求

static func getDotsToDrawRoute(positions : [CLLocationCoordinate2D], completion: @escaping(_ path : GMSPath) -> Void) { 
    if positions.count > 1 { 
     let origin = positions.first 
     let destination = positions.last 
     var wayPoints = "" 
     for point in positions { 
      wayPoints = wayPoints.characters.count == 0 ? "\(point.latitude),\(point.longitude)" : "\(wayPoints)|\(point.latitude),\(point.longitude)" 
     } 
     let request = "https://maps.googleapis.com/maps/api/directions/json" 
     let parameters : [String : String] = ["origin" : "\(origin!.latitude),\(origin!.longitude)", "destination" : "\(destination!.latitude),\(destination!.longitude)", "wayPoints" : wayPoints, "key" : googleAPI_KEY] 
     Alamofire.request(request, method:.get, parameters : parameters).responseJSON(completionHandler: { response in 
      guard let dictionary = response.result.value as? [String : AnyObject] 
       else { 
        return 
      } 
      if let routes = dictionary["routes"] as? [[String : AnyObject]] { 
       if routes.count > 0 { 
        var first = routes.first 
        if let legs = first!["legs"] as? [[String : AnyObject]] { 
         let fullPath : GMSMutablePath = GMSMutablePath() 
         for leg in legs { 
          if let steps = leg["steps"] as? [[String : AnyObject]] { 
           for step in steps { 
            if let polyline = step["polyline"] as? [String : AnyObject] { 
             if let points = polyline["points"] as? String { 
              fullPath.appendPath(GMSMutablePath(fromEncodedPath: points)) 
             } 
            } 
           } 
           completion(path: fullPath) 
          } 
         } 
        } 
       } 
      } 
     }) 
    } 
} 
2

在Swift 3的GoogleMap上的兩個標記之間繪製折線。

// Pass your source and destination coordinates in this method. 
func getPolylineRoute(from source: CLLocationCoordinate2D, to destination: CLLocationCoordinate2D){ 

     let config = URLSessionConfiguration.default 
     let session = URLSession(configuration: config) 

     let url = URL(string: "http://maps.googleapis.com/maps/api/directions/json?origin=\(source.latitude),\(source.longitude)&destination=\(destination.latitude),\(destination.longitude)&sensor=false&mode=driving")! 

     let task = session.dataTask(with: url, completionHandler: { 
      (data, response, error) in 
      if error != nil { 
       print(error!.localizedDescription) 
      }else{ 
       do { 
        if let json : [String:Any] = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]{ 

         let routes = json["routes"] as? [Any] 
         let overview_polyline = routes?[0] as?[String:Any] 
         let polyString = overview_polyline?["points"] as?String 

         //Call this method to draw path on map 
         self.showPath(polyStr: polyString!) 
        } 

       }catch{ 
        print("error in JSONSerialization") 
       } 
      } 
     }) 
     task.resume() 
    } 

在地圖上繪製折線。

func showPath(polyStr :String){ 
    let path = GMSPath(fromEncodedPath: polyStr) 
    let polyline = GMSPolyline(path: path) 
    polyline.strokeWidth = 3.0 
    polyline.map = mapView // Your map view 
} 
0
private func drowRoute(){ 

let path = GMSMutablePath() 
    path.addLatitude(self.lat!, longitude: self.long!) 
    path.addLatitude(self.destLat!, longitude: self.destLong!) 
let polyline = GMSPolyline(path: path) 
polyline.strokeWidth = 2.0 
polyline.strokeColor = UIColor.blue 
polyline.geodesic = true 
polyline.map = mappView 

}