Ios 使用谷歌地图搜索地点后更新相机
我正在使用google map iOS SDK在我们的应用程序中显示地图,我正在从google search place API获取纬度和经度。之后,我想将相机位置更新到搜索的位置,但它不会更新。有人能告诉我哪里错了吗?我的摄像机更新代码如下:Ios 使用谷歌地图搜索地点后更新相机,ios,swift,google-maps,sdk,swift5,Ios,Swift,Google Maps,Sdk,Swift5,我正在使用google map iOS SDK在我们的应用程序中显示地图,我正在从google search place API获取纬度和经度。之后,我想将相机位置更新到搜索的位置,但它不会更新。有人能告诉我哪里错了吗?我的摄像机更新代码如下: let coordinate = CLLocationCoordinate2D(latitude: self.latitude, longitude: self.longitude) let newCamera = GMSCameraUpdate.se
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
中确实有地图的引用,并且它不是零。与自身纬度
和自身经度
类似,并确保此坐标有效coordinate
和newCamera
放在主队列的正上方self.mapView?.animate(使用:newCamera)
animateToLocation
self.mapView?.animate(位置:坐标)
编辑:运行示例应用程序后,以下是我的发现: 你的应用程序在我端引发以下异常: 由于未捕获异常“GMSThreadException”而终止应用程序,原因:“必须从主线程调用API方法” 因此,我无法重现与地图相关的问题,你有关于相机不更新的地方搜索。但这并不是问题所在,因为当您将更新相机的代码放在
viewDidLoad()中时,它可以正常工作
为了解决这个问题,我强烈建议您使用Google的PlacesSDK和Autocomplete,而不是您现有的,请参阅
希望这有帮助 请尝试以下可能的解决方案:
首先,确保您的self.mapView
中确实有地图的引用,并且它不是零。与自身纬度
和自身经度
类似,并确保此坐标有效
将coordinate
和newCamera
放在主队列的正上方self.mapView?.animate(使用:newCamera)
如果上述方法无效,请尝试改用animateToLocation
self.mapView?.animate(位置:坐标)
编辑:运行示例应用程序后,以下是我的发现:
你的应用程序在我端引发以下异常:
由于未捕获异常“GMSThreadException”而终止应用程序,原因:“必须从主线程调用API方法”
因此,我无法重现与地图相关的问题,你有关于相机不更新的地方搜索。但这并不是问题所在,因为当您将更新相机的代码放在viewDidLoad()中时,它可以正常工作
为了解决这个问题,我强烈建议您使用Google的PlacesSDK和Autocomplete,而不是您现有的,请参阅
希望这有帮助 替换这个:
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)
}
为此:
camera = try GMSCameraPosition.camera(withLatitude: self.latitude, longitude: self.longitude, zoom: 14.0)
DispatchQueue.main.async {
CATransaction.begin()
CATransaction.setValue(2, forKey: kCATransactionAnimationDuration)
self.marker.position = CLLocationCoordinate2D(latitude: googleLocation!.lat, longitude: googleLocation!.long)
self.marker.map?.animate(to: camera)
CATransaction.commit()
}
感谢@evan的支持更换此:
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)
}
为此:
camera = try GMSCameraPosition.camera(withLatitude: self.latitude, longitude: self.longitude, zoom: 14.0)
DispatchQueue.main.async {
CATransaction.begin()
CATransaction.setValue(2, forKey: kCATransactionAnimationDuration)
self.marker.position = CLLocationCoordinate2D(latitude: googleLocation!.lat, longitude: googleLocation!.long)
self.marker.map?.animate(to: camera)
CATransaction.commit()
}
感谢@evan的支持请查看我的答案,并让我知道它是否有效。如果没有,我将能够在周一复制这个问题,并进一步提供帮助。@evan,感谢重播。我尝试了你的解决方案,但没用。谢谢你的更新,该死的,我明白了。今天我会在办公室的mac电脑上复制这篇文章,然后回复你。你能从我这边发布复制这篇文章所需的所有相关代码吗?您发布的代码本身就符合文档要求。您好@evan,我正在共享我的谷歌地图代码的谷歌文档链接,请检查searchPlaceFromGoogleMap()函数。请查看我的答案,并告诉我它是否有效。如果没有,我将能够在周一复制这个问题,并进一步提供帮助。@evan,感谢重播。我尝试了你的解决方案,但没用。谢谢你的更新,该死的,我明白了。今天我会在办公室的mac电脑上复制这篇文章,然后回复你。你能从我这边发布复制这篇文章所需的所有相关代码吗?您发布的代码本身就符合文档的要求。嗨@evan,我正在共享我的谷歌地图代码的谷歌文档链接,请检查searchPlaceFromGoogleMap()函数1。po self.mapView▿ 可选-部分:po self.longitude 73.8635912 po self.lation 18.5018322 2。尝试此步骤无效。3.他们(谷歌地图SDK)用animate(toLocation:coordinate)替换animateToLocation,这也不起作用。po self.mapView▿ 可选-部分:po self.longitude 73.8635912 po self.lation 18.5018322 2。尝试此步骤无效。3.他们(谷歌地图SDK)用animate(toLocation:coordinate)替换animateToLocation,这也不起作用。
camera = try GMSCameraPosition.camera(withLatitude: self.latitude, longitude: self.longitude, zoom: 14.0)
DispatchQueue.main.async {
CATransaction.begin()
CATransaction.setValue(2, forKey: kCATransactionAnimationDuration)
self.marker.position = CLLocationCoordinate2D(latitude: googleLocation!.lat, longitude: googleLocation!.long)
self.marker.map?.animate(to: camera)
CATransaction.commit()
}