2016-03-22 92 views
0

我在plist中加入NSLocationWhenInUseUsageDescription并将其设置为YES,这里是我的代码:当前位置9

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let location = locations.last 

    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) 

    self.mapView.setRegion(region, animated: true) 

    print(location) 
    print(center) 

} 

但它仍然无法正常工作。它只是没有显示任何东西。 它看起来像locationManager函数永远不会被调用。它甚至不打印任何内容来记录。

回答

0

就解决它通过添加:

locationManager = CLLocationManager() 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.requestAlwaysAuthorization() 
    locationManager.startUpdatingLocation() 

和声明的LocationManager为类变量。 var locationManager:CLLocationManager!