How to refresh the markers on google maps, without refreshing the map using swift

左心房为你撑大大i 提交于 2019-12-13 03:51:16

问题


Am showing some vehicles on google maps(latitudes & longitudes am getting from API). I am getting latitude & longitudes from storingDataOftripLocDetails() method. And am calling that method for every 2 seconds.

After getting lat & long am showing one marker. and here because of timer my google maps is also refreshing. But my requirement is i have to refresh marker only.

how should i achieve this?

var driverLatitude: Double?
var driverLongitude: Double?

Here in the following method am calling timer

override func viewDidLoad() {
        self.tripLocDetailsTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(tripLocDetailCustomrWebserviceCall), userInfo: nil, repeats: true)
}

Here am getting data from API. and calling showingThePath()

func storingDataOftripLocDetails(_ data: TripLocationDetailsResponse){
        self.driverLatitude = data.locationDetails?.driverDetails?.pickupLatitude
        self.driverLongitude = data.locationDetails?.driverDetails?.pickupLongitude
        self.showingThePath()
}

Here in the following method am showing 1 marker & showing the path

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

来源:https://stackoverflow.com/questions/48166399/how-to-refresh-the-markers-on-google-maps-without-refreshing-the-map-using-swif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!