如何快速沿折线移动标记

时间:2019-02-12 05:31:55

标签: swift

我已经通过json获得了开始和结束位置,但是当我循环播放动画时,它只占用循环的最后一点,并且标记沿直线移动

func playAnimation(){

    let origin = "\(self.locationManager.location?.coordinate.latitude ?? 0),\(self.locationManager.location?.coordinate.longitude ?? 0)"
    let destination = "\(latitude),\(longitude)"


    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=googleApi"
    print(url)
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    guard let requestUrl = URL(string   :url) else { return }
    let request = URLRequest(url:requestUrl)

    let task = session.dataTask(with: request, completionHandler: {
        (data, response, error) in

        DispatchQueue.main.async {
            if error != nil {
                print(error!.localizedDescription)

            }else{
                do {

                    if let json : [String:Any] = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]{

                        if let routes = json["routes"] as? [Any]  {
                            //print(routes)
                            if let route = routes[0] as? [String: Any] {
                                //print(route)
                                if let legs = route["legs"] as? [Any]{
                                    // print(legs)
                                    if let leg = legs[0] as? [String: Any]{
                                        // print(leg)
                                        if let steps = leg["steps"] as? [Any]{
                                            // print("steps:\(steps)")

                                            for step in steps {
                                                //print(step)
                                                let endLocation:NSDictionary = (step as! NSDictionary).value(forKey: "end_location") as! NSDictionary
                                                print("endLocation is : \(endLocation)")

                                                let endLocationLatitude = endLocation.object(forKey: "lat") as! CLLocationDegrees
                                                print(endLocationLatitude)

                                                let endLocationLongitude = endLocation.object(forKey: "lng") as! CLLocationDegrees
                                                print(endLocationLongitude)

                                                let startLocation:NSDictionary = (step as! NSDictionary).value(forKey: "start_location") as! NSDictionary
                                                print("startLocation is : \(startLocation)")

                                                let startLocationLatitude = startLocation.object(forKey: "lat") as! CLLocationDegrees
                                                print(startLocationLatitude)

                                                let startLocationLongitude = startLocation.object(forKey: "lng") as! CLLocationDegrees
                                                print(startLocationLongitude)

// let destinationLocation = CLLocationCoordinate2DMake(19.0178967,72.8558875)

                                               let destinationLocation = CLLocationCoordinate2DMake(endLocationLatitude,endLocationLongitude)

                                                print("destinationLocation:\(destinationLocation)")


                                                let startLocationDestination = CLLocationCoordinate2DMake(startLocationLatitude, startLocationLongitude)

                                              //self.updateMarker(startlocation: startLocationDestination, endlocation: destinationLocation)

                                               //self.updateMarker(coordinates: destinationLocation)
                                                CATransaction.begin()
                                                CATransaction.setAnimationDuration(5.0)
                                                self.marker.position = destinationLocation
                                                self.marker.map = self.mapView

                                                self.delay(2, closure: {})

                                                // Center Map View
                                                let camera = GMSCameraUpdate.setTarget(destinationLocation)
                                                self.mapView.animate(with: camera)

                                                CATransaction.commit()

                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }catch{
                    print("error in JSONSerialization")
                    DispatchQueue.main.async {
                        self.activityIndicator.stopAnimating()
                    }
                }
            }
        }
    })
    task.resume()
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码移动标记。您需要通过位置,并且标记必须出现在地图上。如果标记不存在,则首先需要创建它,然后移动

let position = CLLocationCoordinate2D(latitude: 
self.locationManager.location?.coordinate.latitude ?? 0.0, longitude: self.locationManager.location?.coordinate.longitude ?? 0.0)
CATransaction.begin()
CATransaction.setAnimationDuration(1.0)
myMarker?.position =  position
myMarker?.map = mapView
myMarker?.appearAnimation = .pop
CATransaction.commit()