2015-12-17 49 views
2

我正在構建一個使用iOS的谷歌地圖SDK的移動應用程序,我試圖使用移動設備的陀螺儀數據在街景全景中平移相機。我已經設置了初始位置的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

你好,有關這個的任何答案? – mllm

回答

1

下面的代碼是工作在迅速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) 
      }) 
    } 
0

工作代碼斯威夫特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中,並且應該正確配置你的應用程序。