gpt4 book ai didi

swift - 如何在 swift 中沿着折线移动标记

转载 作者:行者123 更新时间:2023-11-30 10:50:39 24 4
gpt4 key购买 nike

我已经通过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)

//让destinationLocation = CLLocationCooperative2DMake(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()
}

最佳答案

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

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()

关于swift - 如何在 swift 中沿着折线移动标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54643406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com