在获取地图的当前位置时,我不知道如何测试是否成功。
let source = MKMapItem.mapItemForCurrentLocation()
// returns an object with:
// isCurrentLocation = 1
// name="Unknown location"
我可以测试,source.name == "Unknown location"
但这会很糟糕。
那么......在这种情况下我如何检测失败/零?
像这样的东西?
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)
}
}