【问题标题】:iOS MKMapView - stop map view from re-centering to the user's location if the user has panned/dragged the map viewiOS MKMapView - 如果用户平移/拖动地图视图,则停止地图视图重新居中到用户的位置
【发布时间】:2014-03-03 03:30:31
【问题描述】:

我有一个MKMapView,我在其中设置了:

[self.mapView setShowsUserLocation:YES];

我想显示用户的位置,并在他/她移动时更新位置。当didUpdateUserLocation 被调用时,地图似乎会重新定位到用户的位置,即使用户已经平移了应用程序以查看另一个区域。我想要的是能够跟踪用户的位置,同时也让用户探索地图。我的didUpdateUserLocation 目前看起来像这样:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

    _currentLocation = userLocation;

    [self.mapView setRegion:MKCoordinateRegionMake(CLLocationCoordinate2DMake(self.currentLocation.coordinate.latitude + 0.0015, self.currentLocation.coordinate.longitude), MKCoordinateSpanMake(0.01, 0.01)) animated:NO];

    [...]
}

关于我应该做什么来实现我想要的任何想法?

【问题讨论】:

    标签: ios objective-c mkmapview core-location


    【解决方案1】:

    只要您将 MKMapView 的 showsUserLocation 属性设置为 YES,用户位置就会自动更新(参见 Apple Docs)。

    您应该删除“setRegion”行,因为该行以用户位置为中心显示视图。

    【讨论】:

    • 谢谢。你知道在不设置区域的情况下设置 MKMapView 的缩放级别的方法吗?我希望它离用户非常近。我只知道_currentLocationdidUpdateUserLocation 之后。
    • 我通过添加一个 BOOL ivar 来解决它,只在 didUpdateUserLocation 中设置一次 setRegion
    • 如果你想在以用户位置为中心的区域以外的区域设置缩放,可以通过region属性获取地图的实际显示区域。然后您将拥有 CLLocationCoordinate2D 中心和 MKCoordinateSpan 跨度。通过保持中心并修改中心,您可以更改缩放级别。只需使用 setRegion:animated: 设置新区域,就可以缩放了 :)
    【解决方案2】:

    我知道这篇文章早已不复存在,但我想分享一下在寻找许多类似问题的答案时对我有用的方法。他们中的许多人根本没有工作。当我通过电子邮件发送注释的屏幕截图时,我想将我的位置移动到一侧,否则我将不得不缩小,以便我的图钉下方的区域仅占据可用区域的四分之一。

    - (IBAction)beStill:(UIButton *)sender {
    _mapView.showsUserLocation = NO;
    }
    
    - (IBAction)moveOn:(UIButton *)sender {
    _mapView.showsUserLocation = YES;
    }
    

    当我还在整理它时,我在屏幕上放置了两个按钮,但随后重构了 UI,以便用户触摸的最后一个控件也可以安全地推断出他是想要打开还是关闭它。然后我只是将语句滑入这些各自的方法中,并删除了 beStill 和 moveOn 按钮。

    这很重要,因为当您启动应用程序时,您首先要启用该功能,除非您位于非洲西海岸外的大西洋

    这样我就不必对 setRegion 做任何事情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      相关资源
      最近更新 更多