【问题标题】:iOS - Zoom to the current user location at app start upiOS - 在应用程序启动时缩放到当前用户位置
【发布时间】:2012-03-15 23:23:03
【问题描述】:

我想在启动时将地图缩放到当前用户位置。我尝试在 viewDidLoad 处使用 mapView.userLocation.coordinate 检索用户位置,但返回的坐标为 (0,0),可能是因为 MapKit 在启动时没有“找到”用户位置。

我找到了实现方法 didUpdateToLocation 的解决方案。我做了以下事情:

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if ( hasZoomedAtStartUp == NO )
    {
        [self zoomAtStartUp]; // my method to zoom the map
        hasZoomedAtStartUp = YES;
    }
}

我在 .h 文件上创建的 hasZoomedAtStartUp 变量并在 ViewDidLoad 中使用 NO 对其进行了初始化。

这个解决方案工作正常,但我想知道是否有另一种方法可以做到这一点,而不需要 if 语句。该 IF 与启动相关,因此出于性能原因,我想将其删除。

【问题讨论】:

    标签: iphone ios mkmapview core-location


    【解决方案1】:

    我非常怀疑 if 语句失败是您需要担心的性能问题。

    您是否经常需要定位服务?如果没有,当您不再需要更新位置时,调用 stopUpdatingLocation 可能会获得更大的收益。随后,您甚至无法到达 didUpdateToLocation,因为您不再获得新的位置数据。

    【讨论】:

    • 我一直需要定位服务,因为我正在开发(实际上是为了学习)一个基于地图的应用程序。 If 语句不是一个大的性能问题,但是,因为我一直需要它,它可能是。我想知道我是否可以在其他地方执行此任务,因为我只需要一次(在启动时)。
    • @Nick,不,我不同意你的观点,如果有人不需要经常定位服务,那也没问题。我们只需要正确编程 didUpdateToLocation 和 stopUpdatingLocation 方法。我们可以这样做,假设我们在特定(A)上显示 CurrentLocation。那么我们只需要从 ViewLoadingTime 调用 didUpdateToLocation 并在 View 消失时调用 stopUpdatingLocation(-(void)viewWillDisappear:(BOOL)动画方法。)。
    【解决方案2】:

    您现在使用的方法-locationManager:didUpdateToLocation:fromLocation 是处理用户位置的最佳位置。不过,我会做一些与你不同的事情。

    首先,您最好接受第一次位置更新。您可能已经要求一定的准确性,但要求它并不意味着该方法中的newLocation 是最好的。通常,您会得到非常低的准确度,或者是过去某个时间的缓存位置。我要做的是检查新位置的年龄和准确性,并且只有在放大时才可以。

    我要做的另一件事是关闭位置更新,无论是在有高精度更新时,还是在更新开始 30 秒后。设置一个定时器将其关闭,当您将其关闭时,设置一个较长的定时器将其重新打开并再次检查。

    最后,确保您在所有情况下都正确实施了-locationManager:didFailWithError:。它始终是您提交应用程序时测试的内容之一。如果它没有优雅地失败(例如,在飞行模式下),它可能会被拒绝。

    在 Stack Overflow 上搜索执行这些操作的技术和代码。

    【讨论】:

      【解决方案3】:

      您可以随时初始化并开始获取位置更新。 CLLocationManager 将在收到新位置时通知您的委托 并将地图区域上的位置设置为显示

      //Don't Forget To Adopt CLLocationManagerDelegate  protocol
      //set up the Location manager     
      locationManager = [[CLLocationManager alloc] init]; 
      locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
      locationManager.distanceFilter = DISTANCE_FILTER_VALUE;
      locationManager.delegate = self; 
      [locationManager startUpdatingLocation]
      
      //WIll help to get CurrentLocation implement the CLLocationManager  delegate
       - (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation  *)newLocation fromLocation:(CLLocation *)oldLocation
      {
      // use this newLocation .coordinate.latitude
      }
      
      // set Span
      MKCoordinateSpan span; 
      //You can set span for how much Zoom to be display like below
      span.latitudeDelta=.005;
      span.longitudeDelta=.005;
      
      //set Region to be display on MKMapView
      MKCoordinateRegion cordinateRegion;
      cordinateRegion.center=latAndLongLocation.coordinate;
      //latAndLongLocation coordinates should be your current location to be display 
      cordinateRegion.span=span;
      //set That Region mapView 
      [mapView setRegion:cordinateRegion animated:YES];
      

      【讨论】:

        【解决方案4】:

        您可以制作一个“初始”委托实现,您可以在缩放到某个位置后取消注册,并注册您现在不需要整个缩放甚至根本不需要的“普通”委托。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-08-01
          • 2017-05-02
          • 1970-01-01
          • 1970-01-01
          • 2014-04-03
          • 1970-01-01
          • 2011-08-11
          相关资源
          最近更新 更多