gpt4 book ai didi

ios - 在 didupdatelocation 内运行的 Mapview 不会让用户滚动 map

转载 作者:行者123 更新时间:2023-11-30 11:17:14 25 4
gpt4 key购买 nike

我有一个 Google map View ,每当位置管理器收到更新的位置时,该 View 都会以用户位置为中心。不过,我希望用户能够自由地平移/缩放 map ,而相机位置不会每秒左右跳回用户位置。

我已将 .isMyLocationEnabled 设置为 false,因为我设计了一个自定义图标(在我的例子中是一艘船)来代表用户位置。

此外,我创建了自己的按钮,以允许用户平移回用户最后的位置(由于上述原因,我没有使用谷歌内置的位置按钮)。这没有包含在示例代码中,但它工作得很好。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

if let location = locations.last {

self.camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude, longitude: location.coordinate.longitude, zoom: 15)
self.mapViewHere.isMyLocationEnabled = false // don't want the standard blue dot or location button
self.mapViewHere.camera = self.camera

/// plot our custom GMSMarker here
let currentPosition = GMSMarker()
currentPosition.map = nil // clear the last location
currentPosition = GMSMarker(position: location.coordinate)
currentPosition.icon = myCustomMarker.png
currentPosition.rotation = (locationManager.location?.course)!
currentPosition.map = self.mapViewHere


} // end locations.first
}// end location manager

因为我正在使用在 didUpdateLocations 中调用的自定义 GMSMarker 来绘制用户位置,所以我认为停止更新功能不会是答案,因为我希望即使在用户缩放/平移时也能继续这样做。

总而言之;是否可以显示最初以用户位置为中心的 map (并随着设备移动而更新),但每当用户在 map View 周围平移/缩放时,一旦 didUpdateLocations 触发, map View 不应自动返回到用户位置?用户应该能够通过按下自定义按钮来触发返回到初始状态。

我正在使用 Xcode 和 Swift 4 开发我的应用程序。

最佳答案

在珀斯 iOS 开发者小组的指导下解决了我的问题;

首先定义一个 bool 变量;

var auto: Bool = true

监听用户操作并相应地调整变量:

 func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
if gesture {
self.auto = false
print("map view moved, self.auto set to \(self.auto)")
}
}

并在 locationManager didUpdateLocation 中使用此变量,以在 didUpdateLocation 触发时有效忽略用户位置的 map 重绘。

if self.auto {
print("self.auto is true, back to auto-camera-tron")

self.camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude, longitude: location.coordinate.longitude, zoom: 15)
}

只要在 if self.auto 之前声明,我的自定义位置图标就会继续在 didUpdateLocation 上绘制

最后,我可以通过简单地更改 self.auto = true 来将自定义位置按钮设置为返回以用户位置为中心

关于ios - 在 didupdatelocation 内运行的 Mapview 不会让用户滚动 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51631797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com