gmsmapview中的动画折线

时间:2019-06-27 07:48:30

标签: ios swift

如何使用两个坐标在Google地图中绘制动画折线

1 个答案:

答案 0 :(得分:0)

func drawRouteOnMap(_ repeatAnimation:Bool) {
    guard let selectedPickUpLocation =  self.selectedPickUpLocation, let selectedDropLocation = self.selectedDropLocation else {
        return
    }
    self.mapView.clear()
    self.addMarakers()
    let bounds = GMSCoordinateBounds(coordinate: selectedPickUpLocation.coordinate, coordinate: selectedDropLocation.coordinate)
    self.mapView.animate(with: GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: 100, left: 20, bottom: 100, right: 20)))
    print(selectedPickUpLocation.coordinate.bearing(to: selectedDropLocation.coordinate))
    self.mapView.animate(toBearing: selectedDropLocation.coordinate.bearing(to: selectedPickUpLocation.coordinate)-40)
    guard let pLat = selectedPickUpLocation.latitude, let pLong = selectedPickUpLocation.longitude,
        let dLat = selectedDropLocation.latitude, let dLong = selectedDropLocation.longitude else {
            return
    }
    let url = URL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=\(pLat),\(pLong)&destination=\(dLat),\(dLong)&sensor=true&mode=driving&key=\(your apikey)")!

    Alamofire.request(url).responseJSON { response in
        if case .failure(let error) = response.result
        {
            print(error.localizedDescription)
        }
        else if case .success = response.result
        {
            if let JSON = response.result.value as? [String:AnyObject], let status = JSON["status"] as? String
            {
                if status == "OK"
                {
                    if let routes = JSON["routes"] as? [[String:AnyObject]], let route = routes.first, let polyline = route["overview_polyline"] as? [String:AnyObject], let points = polyline["points"] as? String {
                        print(points)
                        DispatchQueue.main.async {
                            let animatedPolyline = AnimatedPolyLine(points,repeats:repeatAnimation)
                            let bounds = GMSCoordinateBounds(path: animatedPolyline.path!)
                            self.mapView.animate(with: GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: 100, left: 20, bottom: 100, right: 20)))
                            animatedPolyline.map = self.mapView
                        }
                    }
                }
                else
                {
                    self.view.showToast(status)
                }
            }
        }
        NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
    }
}