谷歌街景旋转相机与设备陀螺仪
Posted
技术标签:
【中文标题】谷歌街景旋转相机与设备陀螺仪【英文标题】:Google Street View Rotate Camera With Device Gyroscope 【发布时间】:2015-12-17 04:36:21 【问题描述】:我正在使用适用于 ios 的 Google Maps SDK 构建一个移动应用,并尝试使用移动设备的陀螺仪数据在街景视图中围绕全景图平移相机。我已经设置了一个 GMSPanoramaView 和一个带有初始位置的 GMSPanoramaCamera。我在 GMSPanoramaView 上使用 -updateCamera 方法,但无法平滑地平移每个全景图。如果有人知道如何实现此功能,请告诉我。到目前为止,这是我的视图控制器的 -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)
)
)
【问题讨论】:
你好,有关于这个的答案吗??? 【参考方案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)
)
【讨论】:
【参考方案2】: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 中添加一个正确的描述符,说明您为什么需要此数据,并且应该正确配置您的应用程序。
【讨论】:
【参考方案3】:我有简单的方法,在我的项目中我使用“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)
【讨论】:
以上是关于谷歌街景旋转相机与设备陀螺仪的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin Essentials教程陀螺仪Gyroscope