##前提・実現したいこと
GoogleのMaps SDK for iOSを使って表示させたPanoramaView(ストリートビュー)の向いてる方角(heading)を取得したい)
以前こちらで質問したものです。
heading
をpanoView.camera.orientation.heading
で取得します。
これを"リアルタイム"で取得する方法をご教授ください。
*追記
・今回の場合、ストリートビューが動いている間の値(heading)の変更を常に監視したい(指を離した後フェードで動きが止まるまで)。
headingを元にLabelに方角をリアルタイムで表示したいと考えています。
touchesbegan
ではタッチ開始の1度しか値を取得できずうまくいきませんでした。
よろしくお願いします。
##コード
Swift
1import UIKit 2import GoogleMaps 3 4class ViewController: UIViewController { 5 6 @IBOutlet weak var streetView: UIView! 7 8 @objc dynamic var panoView: GMSPanoramaView! 9 @objc dynamic var heading: CLLocationDirection = 0 10 let latitude: CLLocationDegrees = 36.5780574 11 let longitude: CLLocationDegrees = 136.6486596 12 13 14 override func viewDidLayoutSubviews() { 15 let width = <略> 16 let height = <略> 17 let frame = CGRect(x: 0, y: 0, width: width, height: height) 18 19 panoView = GMSPanoramaView(frame: frame) 20 panoView.camera = GMSPanoramaCamera(heading: heading, pitch: -10, zoom: 1) 21 panoView.moveNearCoordinate(CLLocationCoordinate2D(latitude: latitude, longitude: longitude)) 22 streetView.addSubview(panoView) 23 24 heading = panoView.camera.orientation.heading 25 } 26 27 override func viewDidLoad() { 28 super.viewDidLoad() 29 30 self.addObserver(self, forKeyPath: "対象プロパティ", options: [.old, .new], context: nil) // <- ここを以下に訂正 31 panoView.camera.orientation.addObserver(self, forKeyPath: "heading", options: [.old, .new], context: nil) // <- ここでエラー 32 } 33 34 35 override func observeValue(forKeyPath keyPath: String?, of object: Any?, 36 change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) { 37 38 print(change![.oldKey]!) // before 39 print(change![.newKey]!) // after 40 } 41} 42
##試したこと
教えていただいたKVOを試してみました。
panoView.camera.orientation.addObserver(self, forKeyPath: "heading", options: [.old, .new], context: nil)
の.orientation
で
エラーValue of type 'GMSOrientation' has no member 'addObserver
が発生します。
知識不足で申し訳ないのですがご教授いただけると幸いです。
回答3件
あなたの回答
tips
プレビュー