import UIKit
import CoreLocation;
class UserLocation: NSObject {
var userPosition: CLLocationCoordinate2D?
let manager = CLLocationManager()
/// 获取用户位置授权,定位用户当前坐标
func startUserlocation() {
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest; //设置精确度
manager.distanceFilter = 50 ; //重新定位变化距离
if #available(iOS 8, *) { //ios8 或更高
print("request");
//manager.requestWhenInUseAuthorization(); //当app进入前台开始定位
manager.requestAlwaysAuthorization(); //请求始终进行定位
}
if !CLLocationManager.locationServicesEnabled() {
print("设备没开启定位");return;
}
manager.startUpdatingLocation(); //开始定位
print("start");
}
}
extension UserLocation: CLLocationManagerDelegate {
// 地理位置更新
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
if(locations.count <= 0 ){
return;
}
print("定位定位")
let userPos = locations.last! as CLLocation //获得最新一次定位信息
userPosition = userPos.coordinate
manager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
print(status);
}
//定位失败
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error);
}
}
如上:
写了一个location类,然后再界面上点击一个按钮触发定位:
func richScan(){
var test = UserLocation();
test.startUserlocation();
}
但是弹出请求权限接口后 一闪而过,根本来不及点。这是什么情况。。。
PHPz2017-04-17 17:13:37
test
이 출시되었습니다. self.test = UserLocation();
高洛峰2017-04-17 17:13:37
문제가 해결되지 않았다면 http://www.cnblogs.com/huangzs/p/5334130 기사를 읽어보세요... 해결되지 않았다면 개인적으로 메시지를 보내주세요