更改Google Maps SDK for iOS的当前位置图标

时间:2018-03-01 03:10:10

标签: ios swift google-maps google-maps-sdk-ios

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

1 个答案:

答案 0 :(得分:5)

如果您想更改默认用户位置标记,则必须为用户当前位置添加新的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)
    }
}

}