【问题标题】:Location Manager Displays default location even before GPS permissions are given位置管理器甚至在获得 GPS 权限之前就显示默认位置
【发布时间】:2015-11-30 09:52:15
【问题描述】:

这是我的代码:

 override func viewDidLoad() {
    super.viewDidLoad()

    //Setting up the Location Manager.
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()

    //Showing user location on the map as a blue dot.
    self.theMap.showsUserLocation = true

    //Setting the delegate for the map view.
    self.theMap.delegate = self
}


func locationManager(manager: CLLocationManager, didUpdateLocations    locations: [CLLocation]) {

    //Getting the last recorded location.
    let location = locations.last
    let ceter = CLLocationCoordinate2D(latitude:   (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)

    //Setting the region of the map.
    let region = MKCoordinateRegion(center: ceter, span: MKCoordinateSpanMake(0.01, 0.01))

    //Assigning the region to the map.
    self.theMap.setRegion(region, animated: true)

    //Stop updating the location.
    self.locationManager.stopUpdatingLocation()

}


func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    //Getting the center coordinate of the screen.
    let thecenter = mapView.centerCoordinate

    //Storing latitude & longitude in seperate variables.
    let centerLat = thecenter.latitude
    let centerlon = thecenter.longitude

    //Converting CLLocationCoordinate2D to CLLocation.
    let movedMap: CLLocation = CLLocation(latitude: centerLat, longitude: centerlon)

    //Performing Reverse GeoCoding to retrieve address from Coordinates.
    CLGeocoder().reverseGeocodeLocation(movedMap) { (placemarks, error) -> Void in

        //Checking for error.
        if(error != nil) {
            print(error)
        }else{

            //Taking the first coordinates among the lot & storing in variable 'p'.
            if let p = placemarks?.last {

                //Unwrapping Optional Strings.
                let roadno = p.subThoroughfare ?? ""

                //Checking if subThoroughfare exists.
                if(p.subThoroughfare != nil) {

                    //Unwrapping Optional Strings.
                    let thoroughfare = p.thoroughfare ?? ""
                    let subLocality = p.subLocality ?? ""
                    let locality = p.locality ?? ""
                    let administrativeArea = p.administrativeArea ?? ""
                    let postalCode = p.postalCode ?? ""
                    let country = p.country ?? ""

                    let address = " \(roadno) \r \(thoroughfare) \r \(subLocality) \r \(locality) \(administrativeArea) \(postalCode) \r \(country)"
                    print(address)

                   //Assigning the address to the address label on the map.
                   self.addressLabel.text = " \(roadno) \r \(thoroughfare) \r \(subLocality) \r \(locality) \(administrativeArea) \(postalCode) \r \(country)"

                }else{

                    //Unwrapping Optional Strings.
                    let thoroughfare = p.thoroughfare ?? ""
                    let subLocality = p.subLocality ?? ""
                    let locality = p.locality ?? ""
                    let administrativeArea = p.administrativeArea ?? ""
                    let postalCode = p.postalCode ?? ""
                    let country = p.country ?? ""

                    let address = " \(roadno) \r \(thoroughfare) \r \(subLocality) \r \(locality) \(administrativeArea) \(postalCode) \r \(country)"

                    print(address)
                    //Assigning the address to the address label on the map.
                    self.addressLabel.text = " \(thoroughfare) \r \(subLocality) \r \(locality) \(administrativeArea) \(postalCode) \r \(country)"
                }
            }
        }
    }
}

我确定我的错误是 regionDidChangeAnimated() 函数,但我无法理解为什么它是反向地理编码坐标,即使在 GPS 许可之前。

*已编辑

这是我在运行项目时偶尔收到的一条新错误消息。

尝试在不提示位置授权的情况下启动 MapKit 位置更新。必须先调用 -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization]。

Here is the screen shot for the error.

【问题讨论】:

    标签: ios swift mapkit core-location locationmanager


    【解决方案1】:

    好的,所以我找到了解决我所面临问题的方法。我不知道这是否是正确的解决方案,但这样做会使错误消失并且地图工作正常。

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
        let span = MKCoordinateSpanMake(100, 80)
        let region = MKCoordinateRegionMake(coordinate, span)
        self.theMap.setRegion(region, animated: true)
    
    
        //Setting up the Location Manager.
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    
        //Showing user location on the map as a blue dot.
        self.theMap.showsUserLocation = true
    
        //Setting the delegate for the map view.
        self.theMap.delegate = self
    
    }
    

    所以我在这里所做的基本上是将地图初始化为 0 纬度和 0 经度,并在 viewDidLoad() 中设置区域,错误就消失了。如果有任何机会这不是正确的解决方案,请告诉我这个问题的确切解决方案是什么。

    【讨论】:

      【解决方案2】:

      我认为您的问题的很大一部分是您正在开始位置更新并在地图视图中显示当前位置之前确保:

      1. 已为设备启用定位服务
      2. 您的应用有权使用它们

      这就是您收到 MapKit 消息的原因。

      确保在使用定位服务之前满足这两个要求CLLocationManager.locationServicesEnabled()CLLocationManager.authorizationStatus()

      提醒一下(我经常忘记这一点),请确保您的 Info.plist 中有相关的使用描述键,NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription 之一。

      【讨论】:

      • 感谢您的回复....我尝试了您提到的 2 点,但由于某种原因,我仍然遇到相同的错误,并且我在信息中同时给出了 'NSLocationWhenInUseUsageDescription' 和 'NSLocationAlwaysUsageDescription' 描述键.plist
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 2015-01-29
      • 2011-05-04
      相关资源
      最近更新 更多