我正在使用Google Maps SDK for iOS构建移动应用程序,我正在尝试使用移动设备的陀螺仪数据在街景视图中围绕全景平移相机。我已经设置了GMSPanoramaView和带有初始位置的GMSPanoramaCamera。我在GMSPanoramaView上使用方法-updateCamera但无法平滑地浏览每个全景图。如果有人知道如何实现此功能,请告诉我。到目前为止,我的代码是我的viewcontroller的-viewDidLoad部分:
if manager.gyroAvailable {
let queue = NSOperationQueue.mainQueue()
manager.startGyroUpdatesToQueue(queue, withHandler: { (data, error) -> Void in
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
// Update UI
let cameraUpdate = GMSPanoramaCameraUpdate.rotateBy((data?.rotationRate.x.radiansToDegrees)!)
self.panoView.updateCamera(cameraUpdate, animationDuration: 1)
})
})
}
答案 0 :(得分:1)
以下代码在swift 3中工作
if motionManager.isGyroAvailable {
motionManager.startGyroUpdates(to: OperationQueue.main, withHandler: { (gyroData: CMGyroData?, error: Error?) in
let y = gyroData!.rotationRate.y
print("gyrodata: \(y)")
let cameraUpdate = GMSPanoramaCameraUpdate.rotate(by: -CGFloat((gyroData?.rotationRate.y)!))
panoView.updateCamera(cameraUpdate, animationDuration: 1)
})
}
答案 1 :(得分:0)
Swift 4中的工作代码(左右控制向上,因此无论手机指向何处,视图都会更新)
import GoogleMaps
import CoreMotion
class ViewController: UIViewController, GMSPanoramaViewDelegate {
let motionManager = CMMotionManager()
override func loadView() {
let panoView = GMSPanoramaView(frame: .zero)
panoView.delegate = self
self.view = panoView
// you can choose any latitude longitude here, this is my random choice
panoView.moveNearCoordinate(CLLocationCoordinate2D(latitude: 48.858, longitude: 2.284))
motionManager.startDeviceMotionUpdates()
if motionManager.isGyroAvailable {
motionManager.startGyroUpdates(to: OperationQueue.main, withHandler: { (gyroData: CMGyroData?, error: Error?) in
// needed to figure out the rotation
let y = (gyroData?.rotationRate.y)!
let motion = self.motionManager.deviceMotion
if(motion?.attitude.pitch != nil) {
// calculate the pitch movement (up / down) I subtract 40 just as
// an offset to the view so it's more at face level.
// the -40 is optional, can be changed to anything.
let pitchCamera = GMSPanoramaCameraUpdate.setPitch( CGFloat(motion!.attitude.pitch).radiansToDegrees - 40 )
// rotation calculation (left / right)
let rotateCamera = GMSPanoramaCameraUpdate.rotate(by: -CGFloat(y) )
// rotate camera immediately
panoView.updateCamera(pitchCamera, animationDuration: 0)
// for some reason, when trying to update camera
// immediately after one another, it will fail
// here we are dispatching after 1 millisecond for success
DispatchQueue.main.asyncAfter(deadline: .now() + 0.0001, execute: {
panoView.updateCamera(rotateCamera, animationDuration: 0)
})
}
})
}
}
}
extension BinaryInteger {
var degreesToRadians: CGFloat { return CGFloat(Int(self)) * .pi / 180 }
}
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
不要忘记将其放在info.plist
Privacy - Motion Usage Description
在info.plist中为您需要此数据的原因添加一个正确的描述符,并且应该正确配置您的应用程序。
答案 2 :(得分:0)
我有一个简单的方法,在我的项目中,我使用“ CLLocationManagerDelegate”-func locationManager(_ manager:CLLocationManager,didUpdateHeading newHeading:CLHeading)
self.locationManager.startUpdatingHeading() // put this line on viewdidload for example
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
self.panoramaView.animate(to: GMSPanoramaCamera(heading: newHeading.magneticHeading, pitch: newHeading.y, zoom: 1), animationDuration: 0.1)
}