Search code examples
iosswiftuiviewcontrollerviewcontrollergoogle-maps-sdk-ios

Getting and storing position of PanoramaView before changing VC


I'm trying to learn Google Map API and i'm building an app that have two UIViewController. Root UIViewController loads a Google Street View Panorama as shown below:

@IBOutlet weak var viewStreet: UIView!
var panoView: GMSPanoramaView!

override func viewDidLoad() {
    super.viewDidLoad()

    let panoView = GMSPanoramaView(frame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height))
    panoView.delegate = self
    panoView.moveNearCoordinate(CLLocationCoordinate2D(latitude: -33.732, longitude: 150.312))

    viewStreet.addSubview(panoView)
    viewStreet.sendSubviewToBack(panoView)
 }

I have added a UIButton that loads another UIViewController, and I'm willing to add a "Back" button to get back to the first one. Using segues it works fine but panoView is always loaded at latitude: -33.732, longitude: 150.312, as i have initializated it. What i'm trying to achieve is getting back to the lat/long of the last panorama loaded before switching to the second VC, for instance:

viewDidLoad -> lat. x1, long. y1;

moving to another panorama -> lat. x2, long. y2;

changing UIViewController;

back to root VC, panorama lat. and long. should be (x2, y2).

Using didMoveToPanorama method i can get panoID every time PanoramaView changes, the problem is storing it when VC changes. May i use NSUserDefaults or is there a better way? Here is code:

 func panoramaView(view: GMSPanoramaView, didMoveToPanorama panorama: GMSPanorama?) {
    panoID = panorama!.panoramaID
}

Note that method didMoveToPanorama:nearCoordinate: can't be used since it is called only when Panorama has moved due to moveNearCoordinate

On a side note, i've edited title cause it was misleading, and improved post with further informations.


Solution

  • I've found an easier way to achieve this:

    In Main.storyboard: Show segue to "SecondViewController"

    In SecondViewController.swift:

    @IBAction func backToPreviousVC(sender: AnyObject) {
        dismissViewControllerAnimated(true, completion: nil)
    }
    

    This way viewDidLoad is not called and it get back to the last panorama seen.