如何让我的 Google 地图始终跟随用户而不让他们滚动离开
How to make my Google Map follow the user at all times and not let them scroll away
我有一个 GMSMapView 连接到我的 mapView 变量。它已全部设置好,甚至为用户在地图上放置了一条随机多段线。目前我有一个 "navigate" 按钮,但它所做的只是放大用户。我想做到这一点,一旦他们点击 "navigate",它就会让相机跟随用户,而不是让他们滚动离开他们的位置(很像 Google 地图应用程序)。我怎样才能使这成为可能?谢谢!
我假设您已经获得 CLLocationManager 并开始使用它更新用户的位置。完成后,您希望在位置发生变化时在 CLLocationManager 的委托方法中设置 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)
}
}
调整缩放参数以获得您想要的结果。
希望对您有所帮助。
我有一个 GMSMapView 连接到我的 mapView 变量。它已全部设置好,甚至为用户在地图上放置了一条随机多段线。目前我有一个 "navigate" 按钮,但它所做的只是放大用户。我想做到这一点,一旦他们点击 "navigate",它就会让相机跟随用户,而不是让他们滚动离开他们的位置(很像 Google 地图应用程序)。我怎样才能使这成为可能?谢谢!
我假设您已经获得 CLLocationManager 并开始使用它更新用户的位置。完成后,您希望在位置发生变化时在 CLLocationManager 的委托方法中设置 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)
}
}
调整缩放参数以获得您想要的结果。
希望对您有所帮助。