在多个GMSMarker之间创建折线

时间:2018-07-22 12:25:59

标签: ios swift google-maps polyline dispatch

我有两个startMarkers和endMarkers列表,我想先在startMarkers之间绘制Polyline,然后在endMarkers之间绘制Polyline,最后在最后一个起点和第一个目标Markers之间绘制Polyline。 有我的代码用于从Google API获取GMSPath,我正在使用DispatchGroup循环调用API。但是第二次闭包不打电话,我无法绘制折线。

final public func getDotsToDrawRoute(positions : [CLLocationCoordinate2D], completion: @escaping(_ path : [GMSPath]) -> Void) {

  var pathes: [GMSPath] = []
    if positions.count > 1 {
      for index in 0...positions.count - 1 {
        myGroup.enter()
        if index != positions.count - 1 {
      let origin = positions[index]
      let destination = positions[index + 1]
      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" : "AIzaSyAdEzHZfZWyjLMuuW92w5fkR86S3-opIF0"]
      Alamofire.request(request, method:.get, parameters : parameters).responseJSON(completionHandler: { response in
        guard let dictionary = response.result.value as? [String : AnyObject]
          else {
            self.myGroup.leave()
            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(path: GMSMutablePath(fromEncodedPath: points))
                      }
                    }
                  }
                  pathes.append(fullPath)
                  self.myGroup.leave()
                }
              }
            }
          }
        }
      })
          self.myGroup.notify(queue: .main) {
            print(" get all pathhhh")
            completion(pathes)
          }
      }
      }
    }

  }

 fileprivate func drawPolylineBetweenMarkers() {

    mapView.animate(toZoom: 5.0)
    var  locations : [CLLocationCoordinate2D] = []
    if startMarkers.count > 0 {
      for item in startMarkers {
        locations.append(item.position)
      }
    }
    if endMarkers.count > 0 {
      for item in endMarkers {
        locations.append(item.position)
      }
    }
    googleService.getDotsToDrawRoute(positions: locations, completion: { paths in
      if paths.count > 0 {
        self.polyLin = []
        for item in paths {
          let polyline = GMSPolyline(path: item)
          polyline.strokeWidth = 1
          polyline.strokeColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
          self.polyLin.append(polyline)
          OperationQueue.main.addOperation {
            polyline.map = self.mapView
          }
        }
      }
    })
  }

如何解决此问题? 感谢所有答复。

0 个答案:

没有答案