【问题标题】:Google Maps iOS SDK Prevent camera zoom after each location updateGoogle Maps iOS SDK 在每次位置更新后防止相机缩放
【发布时间】:2016-08-03 15:46:53
【问题描述】:

我有一个应用程序需要不断更新用户的位置,以便我可以显示他们当前的坐标和高度。我正在使用 didUpdateLocations 函数执行此操作:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let location = locations.last {
        mapView.camera = GMSCameraPosition(target: locationManager.location!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
        let locValue : CLLocationCoordinate2D = manager.location!.coordinate
        let altitude : CLLocationDistance = Double(round(1000*manager.location!.altitude)/1000)
        let long = Double(round(10000*locValue.longitude)/10000)
        let lat = Double(round(10000*locValue.latitude)/10000)
        let alt = String(altitude) + " m"
        latitudeLabel.text = String(lat)
        longitudeLabel.text = String(long)
        altitudeLabel.text = alt
        showLearningObjectsWithinRange(location)
    }
}

问题是,当我尝试放大地图上的某个位置时,如果我稍微移动设备,相机就会再次缩小。显然这是因为我的 didUpdateLocations 函数中的第一行设置了相机位置,但是如果我删除该行,地图根本不会以它们的位置为中心。

我尝试将 GMSCameraPosition 代码移动到 viewDidLoad、viewWillAppear 和其他几个地方,但这会导致应用崩溃,因为它无法及时找到用户。

有人对如何进行这项工作有任何建议吗?谢谢。

【问题讨论】:

  • zoom: 15 ,使用 map.camera.zoom 或查看文档,以便更新位置时缩放不会改变

标签: ios swift google-maps


【解决方案1】:

用这个代替那条线(mapview.camera = ...)

mapView.animate(toLocation: CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude))

【讨论】:

    【解决方案2】:

    关于实现位置更新,由于位置更新优化并通过线程,GitHub 上发布了一个问题 - Implement Responsive User Location Tracking Mode,在地图上显示用户位置的给定解决方法是调用

    nMapView.setMyLocationEnabled(true);
    

    代替:

    nMapView.setMyLocationTrackingMode(MyLocationTrackingMode.TRACKING_FOLLOW);
    

    然后,关于相机缩放,如Camera and View - Zoom 中所述,您可以尝试设置最小或最大缩放以限制缩放级别。如前所述,

    您可以通过设置最小和最大缩放级别来限制地图可用的缩放范围。

    您也可以尝试这篇 SO 帖子中给出的解决方案 - Google Maps iOS SDK, Getting Current Location of user。希望对您有所帮助。

    【讨论】:

    • 这无关紧要。问题是关于谷歌地图的。
    猜你喜欢
    • 2015-03-15
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多