AM显示在谷歌地图的一些车辆(纬度和经度值是从API获得)。 我正在从纬度和经度storingDataOftripLocDetails()
方法。 和我呼吁每2秒的方法。
越来越之后纬度和长我展示一个标志。 这里,因为我的谷歌地图的计时器也令人耳目一新。 但我的要求是我必须只刷新标记。
我应该如何实现这一目标?
var driverLatitude: Double?
var driverLongitude: Double?
在这里,在下面的方法正在呼叫计时器
override func viewDidLoad() {
self.tripLocDetailsTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(tripLocDetailCustomrWebserviceCall), userInfo: nil, repeats: true)
}
我在这里从API获取数据。 并调用showingThePath()
func storingDataOftripLocDetails(_ data: TripLocationDetailsResponse){
self.driverLatitude = data.locationDetails?.driverDetails?.pickupLatitude
self.driverLongitude = data.locationDetails?.driverDetails?.pickupLongitude
self.showingThePath()
}
在这里,在下面的方法很表示1个标记物表示路径
func showingThePath(){
let vehicleLocation = CLLocationCoordinate2D(latitude: CLLocationDegrees(self.driverLatitude!), longitude: CLLocationDegrees(self.driverLongitude!))
let cabIM = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 15))
cabIM.image = UIImage(named: "bike_color")
let vehicleImage = GMSMarker()
vehicleImage.iconView = cabIM
vehicleImage.infoWindowAnchor = CGPoint(x: 0.44, y: 0.40)
vehicleImage.tracksViewChanges = false
vehicleImage.position = vehicleLocation
vehicleImage.map = self.mapView
let origin = "\(String(describing: driverLatitude)),\(String(describing: driverLongitude))"
let destination = "\(String(describing: pickupLatitude!)),\(String(describing: pickupLongitude!))"
let url = URL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)")
URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
if(error != nil){
}else{
do{
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
print(json)
if json["status"] as! String == "OK"
{
let routes = json["routes"] as! [[String:AnyObject]]
OperationQueue.main.addOperation({
for route in routes
{
let routeOverviewPolyline = route["overview_polyline"] as! [String:String]
let points = routeOverviewPolyline["points"]
let path = GMSPath.init(fromEncodedPath: points!)
self.path = GMSPolyline(path: path)
self.path.strokeColor = .gray
self.path.strokeWidth = 3.0
self.path.map = self.mapView
}
})
}
}catch let error as NSError{
print(error)
}
}
}).resume()
}