Swift GMSPanoramaView (Google Maps Street View) - Add a Compass Button

116 Views Asked by At

I am using a Google Maps Street View in Swift and am trying to find a way to add a compass button or a constant compass.

Simply put, as the user clicks and travels through the streets on the screen, if they hit the compass button at any time it will display their current compass heading (IE: North East).

I have a simple view attached to an outlet:

@IBOutlet weak var panoramaView: GMSPanoramaView!

Loaded like:

panoramaView.moveNearCoordinate(CLLocationCoordinate2D(latitude: latitude, longitude: longitude))

It seems there is an option for a compass if you are using other types of map views but I can find any options for a compass on GMSPanoramaView.

I can get the users current position using:

func panoramaView(_ view: GMSPanoramaView, didMoveTo panorama: GMSPanorama?) {
        //print("Current Position: \(panorama?.coordinate.latitude ?? 0.0)")
        
        latitude = panorama?.coordinate.latitude ?? 0.0
        longitude = panorama?.coordinate.longitude ?? 0.0

    }

The rest I am stumped with. Those seem to be the only variables I can access. Does anyone know how I would move forward and turn this into a compass heading?

Any help would be greatly appreaciated.

1

There are 1 best solutions below

1
Yrll On

use GMSPanoramaCamera Class to obtain the heading values

As per the reference documentation, the GMSPanoramaCamera Class has an orientation property which groups together heading and pitch.

Here's a method you can try out to get the orientation values whenever you move your camera:

  func panoramaView(_ panoramaView: GMSPanoramaView, didMove camera: GMSPanoramaCamera) {
    print("Camera:\(camera.orientation.heading), \(camera.orientation.pitch), \(camera.zoom)")
  }

console output:

Heading, pitch, zoom values

Documentation states that the Heading defines the rotation angle around the camera locus in degrees relative from true north. Headings are measured clockwise: true north is 0, east is 90, south is 180, west is 270.

With this, I believe you will be able to make conditionals to print if it is either North, East, South, West, or anything in between. Then integrate it with your button by storing the latest state.

I hope this helps!