2

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.

Yiags1978
  • 19
  • 3

1 Answers1

0

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!

Yrll
  • 1,619
  • 2
  • 5
  • 19
  • This helps for sure! Thank you mate – Yiags1978 Jul 05 '23 at 07:51
  • 1
    Glad to hear that! If this indeed helped fix your issue, an upvote would be greatly appreciated by the community as this can help others also in the future. :'> – Yrll Jul 05 '23 at 08:12