直接使用 CLLocationManager 时,通常会在第一个回调中获得缓存位置。它通常是一个很好的位置,虽然旧。之后,您可以使用 wifi、手机信号塔(如果可用)快速获得额外的回调,从而提供更好的位置。如果您要求 < 1000m 的精度,您将(在更多秒后)获得 GPS 三角测量。
这些都不应该是不准确的,以至于在海洋中间。我怀疑这行代码:
self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;
正在访问坐标 whileuserLocation
或location
is nil
。如果userLocation
orlocation
为零,这将返回 0 坐标。lat=0, lon=0 的位置在大西洋,非洲海岸附近。在从中获取坐标之前,您可以添加一个检查location
以确保它不为零,即:
if (self.mapView.userLocation.location) {
self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;
[mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate zoomLevel:ZOOM_LEVEL animated:YES];
}
您还需要等待对MKMapViewDelegate mapView:didUpdateUserLocation:
的回调以了解何时有可用的有效位置。您的实现didUpdateUserLocation:
应该丢弃任何具有表示无效位置的水平精度< 0 的位置。
-(void)mapView:(MKMapView*)mapView didUpdateUserLocation:(MKUserLocation*)userLocation
{
if (userLocation.location.horizontalAccuracy > 0) {
[mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate zoomLevel:ZOOM_LEVEL animated:YES];
}
}