我在谷歌地图上显示了一些车辆(来自API的纬度和经度)。
我正在获得纬度和来自storingDataOftripLocDetails()
方法的经度。我每隔2秒就调用一次该方法。
获得lat&我正在展示一个标记。而这里因为计时器我的谷歌地图也令人耳目一新。但我的要求是我只需刷新标记。
我应该如何实现这一目标?
var driverLatitude: Double?
var driverLongitude: Double?
以下方法调用timer
override func viewDidLoad() {
self.tripLocDetailsTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(tripLocDetailCustomrWebserviceCall), userInfo: nil, repeats: true)
}
这是从API获取数据。并调用showsThePath()
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()
}