使用MapKit遇到错误。 [无法检索CarrierName。]

时间:2017-01-02 07:44:10

标签: swift background mapkit

我收到此错误消息:

2017-01-02 15:29:07.886534 TimeStamp [665:191103] [LogMessageLogging] 6.1无法检索CarrierName。 CTError:domain-2,code-5,errStr:((os / kern)失败)

我想我想让应用程序每隔一分钟将我的位置更新到服务器。

我还在plist上设置了这两个键:

Privacy - Location Always Usage Description
Privacy - Location When In Use Usage Description

并在“capibilities”中启用“后台模式”,“位置更新”

我用谷歌搜索,发现很多人在将Xcode更新为8.1后遇到了这个问题。并且只有在使用MapKit时才会出现此问题。我想知道如何解决它?我是新手。我希望有人可以帮我解决这个问题。非常感谢你。

import UIKit
import MapKit
import APScheduledLocationManager
import CoreLocation

class MapViewController: UIViewController, MKMapViewDelegate, APScheduledLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!

    var manager: APScheduledLocationManager!
    let radius = 1000.0

    let locations = [
        ["title": "New York, NY", "latitude": 40.713054, "longitude": -74.007228, "image": UIImage(named: "tram")?.resizeImage(newWidth: 40) as Any],
        ["title": "Los Angeles, CA", "latitude": 34.052238, "longitude": -118.243344, "image": UIImage(named: "dark")?.resizeImage(newWidth: 40) as Any],
        ["title": "Chicago, IL",     "latitude": 41.883229, "longitude": -87.632398, "image": UIImage(named: "star")?.resizeImage(newWidth: 40) as Any],
        ["title": "Home", "latitude": 22.2831319172855, "longitude": 114.126703455693, "image": UIImage(named: "work")?.resizeImage(newWidth: 40) as Any], 
        ["title": "Max", "latitude": 22.282369430502, "longitude": 114.127022599629, "image": UIImage(named: "home")?.resizeImage(newWidth: 40) as Any],    
        ["title": "Ann's home", "latitude": 22.3597674705159, "longitude": 114.127513034534, "image": UIImage(named: "logo")?.resizeImage(newWidth: 40) as Any]
]

    override func viewDidLoad() {
        super.viewDidLoad()

        manager = APScheduledLocationManager(delegate: self)

        mapView.delegate = self
        mapView.showsUserLocation = true

        switch CLLocationManager.authorizationStatus() {
        case .authorizedAlways:
            manager.startUpdatingLocation(interval: 60, acceptableLocationAccuracy: 100)
        default:
            manager.requestAlwaysAuthorization()
        }
    }

    @IBAction func changeMapType(_ sender: Any) {
        if mapView.mapType == MKMapType.standard {
            mapView.mapType = MKMapType.satellite
        } else if mapView.mapType == MKMapType.satellite {
            mapView.mapType = MKMapType.hybrid
        } else if mapView.mapType == MKMapType.hybrid {
            mapView.mapType = MKMapType.standard
        }
    }

    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        let photoAnnotations = locations.map {
            PhotoAnnotation(title: $0["title"] as? String, image: $0["image"] as? UIImage, coordinate: CLLocationCoordinate2D(latitude: $0["latitude"] as! CLLocationDegrees, longitude: $0["longitude"] as! CLLocationDegrees))
        }
        mapView.addAnnotations(photoAnnotations)
    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if let annotation = annotation as? PhotoAnnotation {
            let identifier = "pin"
            let view: PhotoAnnotationView
            if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? PhotoAnnotationView {
                dequeuedView.annotation = annotation
                dequeuedView.image = annotation.image
                view = dequeuedView
            } else {
                view = PhotoAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                view.canShowCallout = false
                view.image = annotation.image
            }
            return view
        }
        return nil
    }

    func scheduledLocationManager(_ manager: APScheduledLocationManager, didChangeAuthorization status: CLAuthorizationStatus){
        switch status {
        case .notDetermined:
            print("NotDetermined")
        case .restricted:
            print("Restricted")
        case .denied:
            print("Denied")
        case .authorizedAlways:
            print("AuthorizedAlways")
            manager.startUpdatingLocation(interval: 60, acceptableLocationAccuracy: 100)
        case .authorizedWhenInUse:
            print("AuthorizedWhenInUse")
        }
    }

    func scheduledLocationManager(_ manager: APScheduledLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations.last!
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, radius, radius)
        mapView.setRegion(coordinateRegion, animated: true)
        print(locations.last!.coordinate.latitude)
        print(locations.last!.coordinate.longitude)
    }

    func scheduledLocationManager(_ manager: APScheduledLocationManager, didFailWithError error: Error) {
        print("Failed to initialize GPS: ", error)
    }
}

0 个答案:

没有答案