关闭项目后不再记录位置

时间:2018-10-25 01:10:18

标签: ios swift

我创建了一个视图并添加了Mapkit,然后去写了我的代码以获取用户位置并将其显示在地图上。它似乎第一次起作用,在关闭Xcode并打开项目后(通过终端“打开项目名称”),它似乎停止显示该位置,我不确定这是否是错误。我尝试了不同的模拟器,对于iPhone 8 plus,出现以下错误

2018-10-24 21:04:44.112488-0400 Jama3aV2[55977:5431299] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C2.1:2][0x7fbe67d00a00] get output frames failed, state 8196 2018-10-24 21:04:44.112802-0400 Jama3aV2[55977:5431299] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C2.1:2][0x7fbe67d00a00] get output frames failed, state 8196 2018-10-24 21:04:44.116124-0400 Jama3aV2[55977:5431299] TIC Read Status [2:0x0]: 1:57 2018-10-24 21:04:44.116306-0400 Jama3aV2[55977:5431299] TIC Read Status [2:0x0]: 1:57 2018-10-24 21:04:44.404369-0400 Jama3aV2[55977:5431298] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C6.1:2][0x7fbe67f0e490] get output frames failed, state 8196 2018-10-24 21:04:44.404509-0400 Jama3aV2[55977:5431298] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C6.1:2][0x7fbe67f0e490] get output frames failed, state 8196 2018-10-24 21:04:44.404894-0400 Jama3aV2[55977:5431298] TIC Read Status [6:0x0]: 1:57 2018-10-24 21:04:44.405132-0400 Jama3aV2[55977:5431298] TIC Read Status [6:0x0]: 1:57 2018-10-24 21:04:44.626215-0400 Jama3aV2[55977:5431297] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C8.1:2][0x7fbe67d07270] get output frames failed, state 8196 2018-10-24 21:04:44.626364-0400 Jama3aV2[55977:5431297] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C8.1:2][0x7fbe67d07270] get output frames failed, state 8196 2018-10-24 21:04:44.626818-0400 Jama3aV2[55977:5431297] TIC Read Status [8:0x0]: 1:57 2018-10-24 21:04:44.626962-0400 Jama3aV2[55977:5431297] TIC Read Status [8:0x0]: 1:57

我不确定自己在做什么错,我在info.plist中添加了“ privacy:location In Use Use Description”。这是我的代码:

``导入UIKit 导入MapKit 导入CoreLocation

MapViewController类:UIViewController,MKMapViewDelegate {

@IBOutlet weak var addressTextView: UITextField!
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
let regionInMeters: Double = 1000

override func viewDidLoad() {
    super.viewDidLoad()
    checkLocationServices()


}


func setupLocationManager(){
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
}


func checkLocationServices() {
    if CLLocationManager.locationServicesEnabled() {
        setupLocationManager()
        checkLocationAuthorization()
    } else {


    }
}

func centerViewLocationServices() {
    if let location = locationManager.location?.coordinate {
        let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters , longitudinalMeters: regionInMeters)
        mapView.setRegion(region, animated: true)
    }
}

func checkLocationAuthorization() {
    switch CLLocationManager.authorizationStatus() {
    case .authorizedWhenInUse:
        mapView.showsUserLocation = true
        centerViewLocationServices()
        locationManager.startUpdatingLocation()
        break
    case .denied:
        break
    case .notDetermined:
        locationManager.requestWhenInUseAuthorization()
        break
    case .restricted:
        break
    case .authorizedAlways:
        break

    }
}

}

扩展名MapViewController:CLLocationManagerDelegate {

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.last else { return }
    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    let region = MKCoordinateRegion.init(center: center, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
    mapView.setRegion(region, animated: true)
}


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

} ``

我遵循了以下youtube教程:

https://www.youtube.com/watch?v=WPpaAy73nJc&lc=z22phjvoivbcuxs4104t1aokgvm4f0nlq11ws3nrrojgbk0h00410.1540259497069676

1 个答案:

答案 0 :(得分:0)

如果要始终在控制台中查看,只需使用打印即可打印出用户所在位置的纬度和经度

在locationManager中执行以下操作:

print(location.coordinate.latitude) 打印(位置。坐标。经度)

哦,是的,永远不要在模拟器中使用位置信息,他们会吮吸,使用合适的手机