我有一个 GMSMapView 连接到我的 mapView 变量。它已全部设置好,甚至为用户在 map 上放置了一条随机多段线。目前我有一个“导航”按钮,但它所做的只是放大用户。我想做到这一点,一旦他们点击“导航”,它就会让相机跟随用户,而不是让他们滚动离开他们的位置(很像谷歌地图应用程序)。我怎样才能使这成为可能?谢谢!
最佳答案
我假设您已经获得了 CLLocationManager 并开始使用它更新用户的位置。完成后,您希望在位置发生变化时在 CLLocationManager 的委托(delegate)方法中设置 mapView 的相机。
大致是这样的:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = manager.location {
// Create camera with the user's new location
let camera = GMSCameraPosition.cameraWithLatitude(location.coordinates.latitude, longitude: location.coordinates.longitude, zoom: 17.0)
// Animate the map to the new camera position
mapView.animateToCameraPosition(camera)
}
}
调整缩放参数以获得您想要的结果。
希望对您有所帮助。
关于swift - 如何让我的谷歌地图始终跟随用户而不是让他们滚动离开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40707179/