谷歌地图GMSMarker更改相机焦点iOS

3

我遇到了一个问题,就是当弹出任何类型的警告框或者我点击标记并导航到谷歌地图应用程序时,GMSMarker会改变相机的焦点。以下是我的实现方式。我在layoutsubviews方法中添加了谷歌地图容器到我的视图控制器头文件中。不知道出了什么问题。请帮忙解决。

override func viewDidLayoutSubviews()
    {
        super.viewDidLayoutSubviews()

        if mapView == nil
        {
            let camera = GMSCameraPosition.camera(withLatitude: 45.582045, longitude:74.32937, zoom: 14.0)
            mapView = GMSMapView.map(withFrame: CGRect(x: 0, y: 0, width: self.mapContainerView.bounds.size.width, height: self.mapContainerView.bounds.size.height), camera: camera)
            mapView.delegate = self

            do {
                // Set the map style by passing the URL of the local file.
                if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
                    mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
                } else {
                    NSLog("Unable to find style.json")
                }
            } catch {
                NSLog("One or more of the map styles failed to load. \(error)")
            }

            self.mapContainerView.addSubview(mapView)
            mapView.settings.setAllGesturesEnabled(false)
            let marker = AppointmentMapDataManager(mapView: mapView).setAppointmentMarker()
//            let location = GMSCameraPosition.camera(withLatitude: marker.position.latitude,
//                                                  longitude: marker.position.longitude,
//                                                  zoom: 14)
//            mapView.camera = location
            var bounds = GMSCoordinateBounds()
            bounds = bounds.includingCoordinate((marker as AnyObject).position)
            let update = GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: self.mapContainerView.frame.height/2 - 33, left: self.mapContainerView.frame.width/2 - 81, bottom: 0, right: 0))
            mapView.moveCamera(update)
        }
    }

enter image description here

enter image description here


为什么你在viewDidLayoutSubviews中移动你的GMSCamera? - Reinier Melian
我需要关注标记边界。 - Ali Shahid
但请以所选委托方法进行标记。 - Reinier Melian
@ReinierMelian,使用哪种方法? - Ali Shahid
检查我的答案并告诉我 - Reinier Melian
2个回答

1

不要在viewDidLayoutSubView中移动相机,这是错误的方法。使用GMSMapViewDelegate的didTap方法,或者如果您想自动执行,请使用延迟执行。

//method for center camera based in your own code
func centerInMarker(marker: GMSMarker) {
    var bounds = GMSCoordinateBounds()
    bounds = bounds.includingCoordinate((marker as AnyObject).position)
    let update = GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: (self.mapView?.frame.height)!/2 - 33, left: (self.mapView?.frame.width)!/2 - 81, bottom: 0, right: 0))
    mapView?.moveCamera(update)
}

您可以在委托方法中使用它。
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    self.centerInMarker(marker: marker)
    return true
}

或者,当您添加标记时,带有延迟。
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
        self.centerInMarker(marker: marker)
}

1
问题已解决,谢谢。起初在didTap方法中我返回了false,我将其更改为true并包含了moveCamera语句,这样就行了。 - Ali Shahid

0

你应该在 viewDidLoad 方法中添加,并在 viewDidLayoutSubviews 中更新 frame


仍然是一样的。 - Ali Shahid
实际上,只有在UIAlertView上,标记位置才会受到干扰。 - Ali Shahid
您可以在第二个截图中看到,在显示提示消息后,它已经向左移动了。 - Ali Shahid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接