【问题标题】:MapView return to the current location annotation?MapView 返回当前位置标注?
【发布时间】:2012-01-17 23:40:07
【问题描述】:

我要求我的 MapView 显示用户的当前位置,我使用 startUpdatingLocation 的委托从 CLLocationManager 获取它,并且我还向 MapView 添加了一些注释,但问题是当在地图中滚动以显示一些注释时地图返回当前用户的注解

- (void)viewDidLoad
{
     [super viewDidLoad];

     // Get the location if the user
     _locationManager = [[CLLocationManager alloc] init];

     if ([CLLocationManager locationServicesEnabled]) 
     {
        _locationManager.delegate = self;
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [_locationManager startUpdatingLocation];
     }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
      // set the map view to the current location
      MKCoordinateSpan span;
      span.latitudeDelta = 0.01;
      span.longitudeDelta = 0.05;

      MKCoordinateRegion region;
      region.span = span;
      region.center = _locationManager.location.coordinate;

      [self.mapView setRegion:region animated:YES];
}

【问题讨论】:

    标签: iphone cocoa-touch ipad mkmapview cllocationmanager


    【解决方案1】:

    如果获得了更准确或最近的位置,或者如果设备正在移动,则可以多次调用didUpdateToLocation 委托方法。

    由于您在该委托方法中无条件设置地图的区域,因此即使在用户或应用程序已在其他位置平移或缩放地图之后,地图也会在获得更新时始终平移回用户位置。

    在该委托方法中,您可以通过添加 bool ivar 来判断您是否已经缩放,从而使其仅缩放到用户位置一次。

    另一种选择是检查location 的各种属性,例如timestamphorizontalAccuracy,并且仅在这些值处于某个阈值时才返回用户位置。

    【讨论】:

      【解决方案2】:

      您必须调用以下方法[_locationManager stopUpdatingLocation];,如下所示:-

      - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
      {
            [_locationManager stopUpdatingLocation];
      
            // set the map view to the current location
            MKCoordinateSpan span;
            span.latitudeDelta = 0.01;
            span.longitudeDelta = 0.05;
      
            MKCoordinateRegion region;
            region.span = span;
            region.center = _locationManager.location.coordinate;
      
            [self.mapView setRegion:region animated:YES];
      }
      

      因为一旦你开始更新位置......它每次都会调用上面的函数......所以在调用这个函数之后你需要通过调用[_locationManager stopUpdatingLocation];method来停止调用这个函数。

      【讨论】:

        猜你喜欢
        • 2015-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-20
        • 1970-01-01
        • 2015-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多