Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


MonoTouch and the iPhone: The GPS and Compass Functions

For many developers, building for Apple's iPhone means writing Objective-C with the XCode IDE. This means being familiar with a lot of language techniques that have largely been superseded in modern languages like C# and Java. Enter MonoTouch, a framework that lets you build iPhone and iPad applications using C#. This frees you from dealing with memory management, pointers, etc., and instead lets you focus on the application functionality.

MonoTouch is a commercial product that is part of the larger, open source Mono project, Novell's open-source implementation of the .NET standard created by Microsoft and published as a set of international standards. In this article, I examine on the iPhone in MonoTouch by building an app that shows our coordinates in the world, as well as our direction of travel, and which way the phone is pointing.

The iPhone utilizes several hardware and software technologies that let you locate it in the world, tell you which way you're traveling, and which way the phone is pointing. The underlying technologies are fairly complex, but Apple abstracts most of the complexity away and gives you an easy way to determine location, etc.

Under the hood, the iPhone utilizes several location technologies:

  • Wi-Fi Positioning Service (WPS). Available on all iPhones since an early OS update, WPS uses a database lookup of nearby Wi-Fi access point MAC addresses with known locations. It then computes the location based on a proprietary algorithm. In a densely packed urban center, with lots of Wi-Fi access points, the accuracy of WPS is often 20-30 meters.
  • Global Positioning System (GPS). Available since the iPhone 3G model, GPS uses a system of satellites orbiting the earth that broadcast time and location signals to the earth. GPS receives these signals and then performs triangulation based on the latency of the signals. GPS is much more reliable than WPS in that it will work anywhere in the world where the GPS receiver has unobstructed view of at least three satellites. The accuracy of GPS is usually within 20 meters, since the year 2000 (when the U.S. government stopped degrading location data for civilian use).
  • Compass. Available since the iPhone 3Gs, the compass enables the iPhone to know its orientation to the magnetic poles (magnetic heading). Coupled with its location information, it can also tell you it's true heading (based on the known magnetic variation of the location).

Having to work with these directly would be pretty tedious, but fortunately for us, all of this functionality is wrapped up in the CoreLocation API. MonoTouch exposes this API via the MonoTouch.CoreLocation namespace, and the most important class is CLLocationManager.

The general pattern for working with CoreLocation is:

  1. Instantiate a CLLocationManager object
  2. Configure any settings on CLLocationManager such as the accuracy you want, etc.
  3. Assign a delegate to handle the location updates
  4. Tell CLLocationManager to start feeding you location updates
  5. Do something interesting with the location information

Let's do just that, but first, a word about delegates in MonoTouch.

Delegates and Prototypes in MonoTouch

Many of the APIs in CocoaTouch (Apple's UI API for iPhone), and consequently MonoTouch, utilize what Apple calls a "prototype" for callbacks. A prototype is similar to Interfaces in .NET. It defines a contract whereby the caller knows that certain methods will exist on a class that conforms to the contract or interface. For example, you might define an interface, say IPerson that has the method Run(int howFast, int howFar). The difference is, with Apple's API, the methods are actually optional. If the runtime doesn't find the Run method, it doesn't matter; the runtime just won't call it. In MonoTouch, if you want to the runtime to find your method that conforms to their prototype, you "register" it by decorating it with an Export attribute:

[Export ("Run")]
public void DoSomeRunning(int howFastToRun, int howFarToRun) {... }

MonoTouch tries to make this a bit easier on us by providing strongly-typed delegate base classes that already have these methods and their respective Export attributes on them. Notice that our method and parameters didn't have the same naming. That's okay, when you register the method all that matters is the data types passed in and returned.

To use them, we simply write a class that derives from the appropriate base delegate class, override the methods that we care about, and assign our class to the delegate property of whatever class that needs it.

MonoTouch Delegates

You don't have to use a strongly-typed delegates that MonoTouch gives you. You can write your own custom delegate classes and the decorate them with the Export attribute on methods you want to expose to handle call-backs. To do this, simply create your class, mark your methods using the Export attribute, and then set your custom class to the WeakDelegate property of whatever class expects a delegate. For more information, see monotouch.net/Documentation/API_Design.

In the case of CoreLocation, this means deriving from the CLLocationManagerDelegate base class, and overriding the methods UpdatedLocation and UpdatedHeading.

Now that we've gotten all that out of the way, let's actually use the CoreLocation API.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.