【问题标题】:How to get accurate user coordinates and stop location manager如何获取准确的用户坐标并停止位置管理器
【发布时间】:2015-03-18 22:43:43
【问题描述】:

我想获取当前设备位置并将地图缩放到该坐标。
我也不想在缩放到用户位置后停止位置管理器,以防止在用户移动时更改地图可见区域。

...viewDidLoad...{
self.locationManager = CLLocationManager();
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.mapView.showsUserLocation = true;
        self.locationManager.startUpdatingLocation();
}

func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]){
var newRegion = MKCoordinateRegion(center: self.mapView.userLocation.coordinate, span: MKCoordinateSpanMake(0.014, 0.014));
            self.mapView.setRegion(newRegion, animated: true);
            self.locationManager.stopUpdatingLocation();
}

此代码的问题是此代码将地图视图移动到海洋并且坐标有时无效(另一个大陆)。
当我停止位置管理器时,地图视图停留在海洋中:-/
制作我想要的东西的正确方法是什么?

【问题讨论】:

标签: swift ios7 ios8 mkmapview core-location


【解决方案1】:

CLLocationManager 并不总是保证单个委托调用的准确坐标(例如第一次委托调用时)。 newRegion 可能只会给你缓存的位置。

有关 CLLocation 的更多信息,请参阅以下答案:

How to stop multiple times method calling of didUpdateLocations() in ios

CLLocationManager holds cache value

Function "didUpdateToLocation" being called without changes

【讨论】:

    猜你喜欢
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多