myLocationEnabled changing to isMyLocationEnabled on swift when using GoogleMaps

 ̄綄美尐妖づ 提交于 2019-12-11 05:23:22

问题


Im trying to get a users current location to show with google maps API. I have been struggling with this issue for weeks and the code runs but doesn't update to current location when I change the location in the simulator.

My code is below and after looking how other people are doing it they all have mapView.myLocationEnabled = true whereas for me this produces an error saying 'myLocationEnabled has been renamed to 'isMylocationEnabled'. I've seen some up to date posts (from 2 months ago) and everyone is using myLocationEnabled without an error it seems, why am I getting this? could this be the reason why my current location is not updating?

import UIKit
import GoogleMaps

class FirstViewController: UIViewController, CLLocationManagerDelegate {


    @IBOutlet weak var mapView: GMSMapView!


    let locationManager = CLLocationManager()
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        mapView.settings.myLocationButton = true
        mapView.settings.compassButton = true
    }
    override func viewDidAppear(_ animated: Bool) {
        locationManager.requestWhenInUseAuthorization()
    }
    func locationManager(manager: CLLocationManager , didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            locationManager.startUpdatingLocation()


            mapView.isMyLocationEnabled = true

            mapView.settings.myLocationButton = true
        }
    }
    func locationManager(manager: CLLocationManager ,didUpdateLocations locations: [CLLocation]) {
        if let location = locations.first {

            mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)

            let marker = GMSMarker()
            marker.title = "Current Location"
            marker.map = mapView
            locationManager.stopUpdatingLocation()
        }
    }
}

回答1:


SO I FINALLY SOLVED THIS! Basically I updated my existing Podfile with GoogleMaps and GooglePlaces and then updated pod to ensure all the frameworks are working. Then I used this code to get my current location to work

    import UIKit

    import GoogleMaps


    class FirstViewController: UIViewController, CLLocationManagerDelegate {


@IBOutlet weak var mapView: GMSMapView!
var locationManager = CLLocationManager()
var vwGMap = GMSMapView()


override func viewDidLoad() {
    super.viewDidLoad()
    let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 22.300000, longitude: 70.783300, zoom: 10.0)
    vwGMap = GMSMapView.map(withFrame: self.view.frame, camera: camera)
    vwGMap.camera = camera

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
    locationManager.distanceFilter = 500
    locationManager.requestWhenInUseAuthorization()
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

    self.view = vwGMap
    }

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

        if (status == CLAuthorizationStatus.authorizedWhenInUse)

        {
            vwGMap.isMyLocationEnabled = true
        }
    }
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let newLocation = locations.last
        vwGMap.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 15.0)
        vwGMap.settings.myLocationButton = true
        self.view = self.vwGMap
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(newLocation!.coordinate.latitude, newLocation!.coordinate.longitude)
    marker.map = self.vwGMap
    }

}



来源:https://stackoverflow.com/questions/40792362/mylocationenabled-changing-to-ismylocationenabled-on-swift-when-using-googlemaps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!