2

マップの現在の位置を取得するときに成功しないかどうかをテストする方法がわかりません。

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

私はテストすることができましたが、それはひどく悪いsource.name == "Unknown location"でしょう.

では...この場合、どのように失敗/nilを検出しますか?

4

1 に答える 1

0

このようなもの ?

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

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

ビューでDidLoad:

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)
        }

    }
于 2015-08-14T14:07:46.607 に答える