1

I'm just curious if anybody knows how to change the current location icon provided by the Google Maps SDK for iOS. I want to change it to allow compass heading rotation.

Justinos
  • 21
  • 1
  • 2

1 Answers1

8

If you want to change default user location marker then you have to add a new GMSMarker for user current location and also update it whenever the user location changes.

You can get user heading by CLLocation object i.e. location.course and pass it to the GMSMarker object in marker.rotation

class ViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {

    @IBOutlet weak var googleMap: GMSMapView!

    var locationManager: CLLocationManager!
    var currentLocationMarker: GMSMarker?
    var mapBearing: Double = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        locationManager = CLLocationManager()
        locationManager.delegate = self

        googleMap.isMyLocationEnabled = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidAppear(_ animated: Bool) {
        if CLLocationManager.locationServicesEnabled() {
            startMonitoringLocation()
            addCurrentLocationMarker()
        }
    }

    func startMonitoringLocation() {
        if CLLocationManager.locationServicesEnabled() {
            locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
            locationManager.activityType = CLActivityType.automotiveNavigation
            locationManager.distanceFilter = 1
            locationManager.headingFilter = 1
            locationManager.requestWhenInUseAuthorization()
            locationManager.startMonitoringSignificantLocationChanges()
            locationManager.startUpdatingLocation()
        }
    }

    func stopMonitoringLocation() {
        locationManager.stopMonitoringSignificantLocationChanges()
        locationManager.stopUpdatingLocation()
    }

    func addCurrentLocationMarker() {
        currentLocationMarker?.map = nil
        currentLocationMarker = nil
        if let location = locationManager.location {
            currentLocationMarker = GMSMarker(position: location.coordinate)
            currentLocationMarker?.icon = UIImage(named: "yourImage")
            currentLocationMarker?.map = googleMap
            currentLocationMarker?.rotation = locationManager.location?.course ?? 0
        }
    }

    func zoomToCoordinates(_ coordinates: CLLocationCoordinate2D) {
        let camera = GMSCameraPosition.camera(withLatitude: coordinates.latitude, longitude: coordinates.longitude, zoom: 20)
        googleMap.camera = camera
    }

    //MARK:- Location Manager Delegate

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("location manager erroe -> \(error.localizedDescription)")
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case .notDetermined:
            break
        case .restricted:
            break
        case .denied:
            stopMonitoringLocation()
            break
        default:
            addCurrentLocationMarker()
            startMonitoringLocation()
            break
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let lastLocation = locations.last {
            currentLocationMarker?.position = lastLocation.coordinate
            currentLocationMarker?.rotation = lastLocation.course
            self.zoomToCoordinates(lastLocation.coordinate)
        }
    }
}    
Keyur
  • 39
  • 5
honey
  • 91
  • 1
  • 4