Book Image

iOS Development with Xamarin Cookbook

By : Dimitrios Tavlikos (USD)
Book Image

iOS Development with Xamarin Cookbook

By: Dimitrios Tavlikos (USD)

Overview of this book

Table of Contents (22 chapters)
iOS Development with Xamarin Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Determining heading


In this recipe, we will learn how to use the built-in compass to determine the heading of the device.

Getting ready

Create a new Single View Application in Xamarin Studio and name it HeadingApp. Just as you did in the previous recipe, add two buttons and a label on the view of the controller.

Note

The project in this recipe cannot be tested on the simulator. A device with compass hardware (magnetometer) is required.

How to do it...

Perform the following steps to determine the heading of the device:

  1. Add the following code in the HeadingAppViewController class:

    private CLLocationManager locationManager;
    public override void ViewDidLoad ()
    {
      base.ViewDidLoad ();
      // Perform any additional setup after loading the view, typically from a nib.
      this.locationManager = new CLLocationManager();
      this.locationManager.UpdatedHeading += LocationManager_UpdatedHeading;
      this.locationManager.Failed += (sender, e) => Console.WriteLine("Failed! {0}", e.Error.LocalizedDescription);
    
    ...