【问题标题】:Changing current location icon for Google Maps SDK for iOS [duplicate]更改适用于 iOS 的 Google Maps SDK 的当前位置图标 [重复]
【发布时间】:2018-03-01 03:10:09
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      相关资源
      最近更新 更多