【问题标题】:How do you set MKMapView region on current location and follow user location?如何在当前位置设置 MKMapView 区域并关注用户位置?
【发布时间】:2019-07-11 02:13:43
【问题描述】:

我有一个按钮,可以将地图居中并根据用户的当前位置设置区域,然后设置要遵循的用户跟踪模式。我在移动时跟随用户,如果他们移动地图或缩小地图,它会将用户跟踪模式设置为无。

这就是我所拥有的

guard let currentLocation = locationManager.location else { return }
let coordinateRegion = MKCoordinateRegion(center: currentLocation.coordinate,
                                          latitudinalMeters: regionRadius,
                                          longitudinalMeters: regionRadius)
map.setRegion(coordinateRegion, animated: true)
map.setUserTrackingMode(MKUserTrackingMode.follow, animated: true)

它成功地将区域设置回用户的当前位置,然后是用户跟踪模式以跟随但由于某种原因它不再跟随。

【问题讨论】:

  • 您的设备如何知道用户的位置已更改?

标签: ios swift mapkit


【解决方案1】:

您需要等待 setRegion 动画完成,以免它与 setUserTrackingMode 混淆。

guard let currentLocation = locationManager.location else { return }
let coordinateRegion = MKCoordinateRegion(center: currentLocation.coordinate, latitudinalMeters: regionRadius, longitudinalMeters: regionRadius)

MKMapView.animate(withDuration: 0.5, animations: {
    self.map.setRegion(coordinateRegion, animated: true)
}) { _ in
    self.map.setUserTrackingMode(MKUserTrackingMode.follow, animated: false)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 2013-01-30
    • 2020-02-20
    • 2014-08-21
    • 1970-01-01
    相关资源
    最近更新 更多