我正在使用Google Map iOS iOS SDK在我们的应用程序中显示地图,我正在从Google Search Place API获取经度和纬度。之后,我想将相机位置更新到搜索到的位置,但是没有更新。有人可以告诉我哪里错了吗?我的更新相机代码如下:

let coordinate = CLLocationCoordinate2D(latitude: self.latitude, longitude:  self.longitude)
let newCamera = GMSCameraUpdate.setTarget(coordinate)
DispatchQueue.main.async {
    CATransaction.begin()
    CATransaction.setValue(2, forKey: kCATransactionAnimationDuration)
    self.marker.position = CLLocationCoordinate2D(latitude: googleLocation!.lat, longitude: googleLocation!.long)
    CATransaction.commit()
    self.mapView?.animate(with: newCamera)
}


我也尝试过

self.mapView?.camera = newCamera


self.mapView?.animate(to: newCamera)


self.mapView?.moveCamera(newCamera)


相同的行为

最佳答案

您能否请尝试以下潜在解决方案:


首先,请确保您实际上使用self.mapView引用了地图,并且引用不为零。对于self.latitudeself.longitude同样,并确保此坐标有效。
coordinatenewCamera放在主队列中,在self.mapView?.animate(with: newCamera)的正上方。


    DispatchQueue.main.async {
        CATransaction.begin()
        CATransaction.setValue(2, forKey: kCATransactionAnimationDuration)
        self.marker.position = CLLocationCoordinate2D(latitude: googleLocation!.lat, longitude: googleLocation!.long)
        CATransaction.commit()
        let coordinate = CLLocationCoordinate2D(latitude: self.latitude, longitude:  self.longitude)
        let newCamera = GMSCameraUpdate.setTarget(coordinate)
        self.mapView?.animate(with: newCamera)
    }



如果上述方法不起作用,请尝试使用animateToLocation

self.mapView?.animate(toLocation: coordinate)




编辑:运行示例应用程序后,这些是我的发现:

您的应用在我端抛出以下异常:


  由于未捕获的异常“ GMSThreadException”而终止应用程序,原因:“必须从主线程调用API方法”


因此,我无法重现您关于相机在地点搜索时未更新的地图相关问题。但这不是这里的问题,因为当放置在viewDidLoad()中时,用于更新相机的代码可以正常工作。

要解决此问题,我强烈建议您使用具有自动完成功能的Google地方信息SDK,而不要使用其中的功能,请参见https://developers.google.com/places/ios-sdk/autocomplete

希望这可以帮助!

07-28 02:31
查看更多