更改iOS版谷歌地图SDK的当前位置图标

更改iOS版谷歌地图SDK的当前位置图标,ios,swift,google-maps,google-maps-sdk-ios,Ios,Swift,Google Maps,Google Maps Sdk Ios,我只是好奇是否有人知道如何更改Google Maps SDK为iOS提供的当前位置图标。我想更改它以允许罗盘航向旋转 如果要更改默认用户位置标记,则必须为用户当前位置添加一个新的GMSMarker,并在用户位置更改时进行更新 您可以通过CLLocation对象(即location.course)获取用户标题,并将其传递给marker.rotation中的GMSMarker对象 class ViewController: UIViewController, CLLocationManagerDel

我只是好奇是否有人知道如何更改Google Maps SDK为iOS提供的当前位置图标。我想更改它以允许罗盘航向旋转

如果要更改默认用户位置标记,则必须为用户当前位置添加一个新的GMSMarker,并在用户位置更改时进行更新

您可以通过CLLocation对象(即location.course)获取用户标题,并将其传递给marker.rotation中的GMSMarker对象

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

如果要更改默认用户位置标记,则必须为用户当前位置添加一个新的GMSMarker,并在用户位置更改时进行更新

您可以通过CLLocation对象(即location.course)获取用户标题,并将其传递给marker.rotation中的GMSMarker对象

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