Caleb
Caleb

Reputation: 305

Google map flickering on update current user location

I am using Google maps to draw path between start and destination location. its working perfectly, when user travel, on update current location and draw route, using this delegate function.

public func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {

   drawPath() 

}

Maps get flickering i am using this function to draw path

func drawPath()
{
   let urlString = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving"

    let url = URL(string: urlString)
    URLSession.shared.dataTask(with: url!, completionHandler: {
        (data, response, error) in

        if(error != nil){
            print("error")
        }else{
            DispatchQueue.main.async {
            do{
                let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
                let routes = json["routes"] as! NSArray
                self.googleMapView?.clear()

                    for route in routes
                    {
                        let routeOverviewPolyline:NSDictionary = (route as! NSDictionary).value(forKey: "overview_polyline") as! NSDictionary
                        let points = routeOverviewPolyline.object(forKey: "points")
                        let path = GMSPath.init(fromEncodedPath: points! as! String)
                        let polyline = GMSPolyline.init(path: path)
                        polyline.strokeWidth = 8
                        polyline.strokeColor = #colorLiteral(red: 0.2420450151, green: 0.487836957, blue: 0.9020499587, alpha: 1)
                        let bounds = GMSCoordinateBounds(path: path!)
                        self.googleMapView!.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 30.0))

                        polyline.map = self.googleMapView
                    }
            }catch let error as NSError{
                print("error:\(error)")
            }
                self.addMarker(loc: desti)

            }
        }
    }).resume()
}

In other case without delegate functions travelled route polyline not clearing, but current location updating.

Can someone suggest a way, Thanks.

Upvotes: 0

Views: 2440

Answers (1)

Rahul Kasar
Rahul Kasar

Reputation: 20

I suggest you to draw route just once either in viewdidload and just update the marker position on location change. So this would remove the route drawing flicker and just move the market on position change.

Upvotes: 1

Related Questions