如何沿折线迅速移动标记

问题描述 投票:0回答:1

我已经获得通过JSON的起点和终点的位置,但是当我通过循环来绘制动画,它需要循环的唯一最后一点,并在一条直线上移动游标

烂花Playnimsn(){

    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)

//让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()
}
swift
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.