检测mapItemForCurrentLocation何时无法找到您的设备

时间:2015-08-14 13:49:50

标签: ios mapkit mkmapitem

在获取地图的当前位置时,我无法弄清楚如何测试是否缺乏成功。

let source = MKMapItem.mapItemForCurrentLocation()
// returns an object with:
//   isCurrentLocation = 1
//   name="Unknown location"

我可以测试source.name == "Unknown location",但那会是可怕的糟糕

那么......在这种情况下如何检测失败/无?

1 个答案:

答案 0 :(得分:0)

这样的东西?

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

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

在viewDidLoad中:

locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

...

代表

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

        if (!locations.isEmpty)
        {

            let myLocation  = locations[0] as! CLLocation

            mapView.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude),
                MKCoordinateSpanMake(0.06, 0.06)), animated: true)
        }

    }