谷歌地图GMSMarker在iOS上改变相机焦距
我遇到了一个问题,GMSMarker会在任何类型的弹出警报上改变相机焦点,或者每当我点击标记器,应用程序就会导航到谷歌地图应用程序。下面是我的实现。我在layoutsubviews方法中将google地图容器添加到我的viewcontroller标题中。不知道发生了什么事。请帮忙谷歌地图GMSMarker在iOS上改变相机焦距,ios,swift,google-maps,google-maps-markers,Ios,Swift,Google Maps,Google Maps Markers,我遇到了一个问题,GMSMarker会在任何类型的弹出警报上改变相机焦点,或者每当我点击标记器,应用程序就会导航到谷歌地图应用程序。下面是我的实现。我在layoutsubviews方法中将google地图容器添加到我的viewcontroller标题中。不知道发生了什么事。请帮忙 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() if mapView == nil
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)
}
}
您应该添加
viewdiload
方法,并在viewdilayoutsubview
中更新frame
,而不是在viewdilayoutsubview中移动相机,这是一种错误的方法,请使用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)
}
为什么要在viewDidLayoutSubViews中移动GMSCamera?我需要关注标记边界。但请使用标记选定的委托方法@ReinierMelian哪个方法?检查我的答案,并让我知道仍然相同。实际上,标记位置仅在UIAlertView上受到干扰。在显示后的第二个屏幕截图中,您可以看到它已移到左侧警报消息。解决了我的问题。谢谢起初,我在didTap方法中返回false,我将其改为true,并包含moveCamera语句。成功了。