为什么 Here iOS Explore 的缩放级别在下面的代码中没有改变?事实上,在 zoomTo() 函数之后,相机正在放大地图

Why is the zoom level for Here iOS Explore not changing in the code below? In fact the camera is zooming in on the map after the zoomTo() func

初始缩放值和最终缩放值是16.8...我检查过了。我在 Here Explore 的文档中发现了一些可能相关的内容:

Any method that modifies the state of the camera will be enqueued and the state will only be updated after drawing the next frame. https://developer.here.com/documentation/ios-sdk-explore/4.11.2.0/api_reference/Classes/MapCamera.html Could this note be related to this situation?

import heresdk
import UIKit

class ViewController: UIViewController {
   
    

    var mapView : MapView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Initialize MapView without a storyboard.
        mapView = MapView(frame: view.bounds)
        view.addSubview(mapView)

        // Load the map scene using a map scheme to render the map with.
        mapView.mapScene.loadScene(mapScheme: MapScheme.normalDay, completion: onLoadScene)
    }

    
    
   
    
    
    // Completion handler when loading a map scene.
    private func onLoadScene(mapError: MapError?) {
        guard mapError == nil else {
            print("Error: Map scene not loaded, \(String(describing: mapError))")
            return
        }

        // Configure the map.
        let camera = mapView.camera
                
//                mapView.mapScene.setLayerVisibility(layerName: MapScene.Layers.landmarks, visibility: VisibilityState.visible)
                
        
        let coordinates = [GeoCoordinates(latitude: 51, longitude: 10),
                               GeoCoordinates(latitude: 49, longitude: 13.3946),
                               GeoCoordinates(latitude: 52.53894, longitude: 13.39194),
                               GeoCoordinates(latitude: 52.54014, longitude: 13.37958)]

        if let box =  GeoBox.containing(geoCoordinates: coordinates) {

            
            var cameraUpdate = MapCameraUpdateFactory.lookAt(area: box)
      
            camera.applyUpdate(cameraUpdate)

            var zoomLevel = camera.state.zoomLevel
           
           //zoom level must decrease here according to me but it actually increases  
            camera.zoomTo(zoomLevel: zoomLevel - 3)
           
            var finalzoom = camera.state.zoomLevel
            
            let geoPolyline = try! GeoPolyline(vertices: coordinates)
                let lineColor = UIColor(red: 0, green: 0.56, blue: 0.54, alpha: 0.63)
                let mapPolyline = MapPolyline(geometry: geoPolyline,
                                              widthInPixels: 30,
                                              color: lineColor)
            mapView.mapScene.addMapPolyline(mapPolyline)

        }
        
        
        
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        mapView.handleLowMemory()
    }
}

您的观察是正确的。相机调用可以入队,但只会在下一帧后生效。

在你的情况下,你尝试更改缩放两次。

  1. camera.applyUpdate(cameraUpdate) // 这会放大到很远以包含设置框。
  2. camera.zoomTo(zoomLevel: zoomLevel - 3) // 这不是基于 1) 的结果执行的,因为它采用当前状态:camera.state.zoomLevel

所以,基本上,缩放级别的第二次更改是:

var zoomLevel = camera.state.zoomLevel // This is still default zoom level.
camera.zoomTo(zoomLevel: zoomLevel - 3)

因此,您的新缩放级别将为 16。步骤 1) 中的排队框更新将被您的代码覆盖。

您正在尝试做的是等待框缩放级别更改的结果,采用生成的缩放级别并将其减少 3。这是您无法排队的内容,因为您必须等待下一个帧完成。

只能等到MapIdleListener完成,第一次缩放操作完成。然后您可以采用生成的新缩放级别并减去 3 - 或者,更好的是,您可以尝试另一种方法:

        // Zoom to the box and add a padding of 100 pixels.
        let orientation = GeoOrientationUpdate(bearing: camera.state.orientationAtTarget.bearing,
                                               tilt: camera.state.orientationAtTarget.tilt)
        let origin = Point2D(x: 100, y: 100)
        let sizeInPixels = Size2D(width: mapView.viewportSize.width - 200, height: mapView.viewportSize.height - 200)
        let mapViewportWithPadding = Rectangle2D(origin: origin, size: sizeInPixels)
        let cameraUpdate = MapCameraUpdateFactory.lookAt(area: box,
                                                         orientation: orientation,
                                                         viewRectangle: mapViewportWithPadding)
        camera.applyUpdate(cameraUpdate)

        let geoPolyline = try! GeoPolyline(vertices: coordinates)
        let lineColor = UIColor(red: 0, green: 0.56, blue: 0.54, alpha: 0.63)
        let mapPolyline = MapPolyline(geometry: geoPolyline,
                                      widthInPixels: 30,
                                      color: lineColor)
        mapView.mapScene.addMapPolyline(mapPolyline)

在这里您无需等待生成的缩放级别,您可以使用一些额外的填充缩放到框 - 这也会降低缩放级别,但结果更加一致和可预测。