Chapter 7



  • Getting to know the compass
  • Using Compass methods
  • Using the Compass options

In Chapter 6, you learned how to use the accelerometer. In this chapter, you learn how to use a device’s built-in compass.


In this section, you learn what the compass is, and how you can use it.

What Is the Compass?

Simply put, the compass built into your device obtains the direction in which the device is pointing. Ideally, if you’re running the PhoneGap Compass API, whichever direction you turn the device, you’ll get back a heading (a number between 0 and 359.99) that corresponds to the direction in which you’re pointing.

Specifically, your device has a solid-state or digital compass. It uses two or three (depending on the model of your device) magnetic field sensors to supply data back to your device. The device uses this data to provide a correct heading with some rapid trigonometric calculations.

If your device is any of the following, it also contains a magnetometer:

  • HTC HD2
  • HTC Dream
  • HTC Desire HD
  • HTC Evo
  • HTC Wildfire
  • Apple
  • Sony Ericsson Xperia X8
  • Sony Ericsson Xperia X10
  • iPhone 4
  • Motorola Droid[4]
  • Motorola Quench
  • Motorola Atrix 4g
  • Nokia N97
  • Nokia E72
  • Nokia N8
  • Blackberry Torch
  • Samsung Galaxy S
  • Samsung Nexus One
  • Samsung Nexus S

A magnetometer can measure the strength and/or direction of a magnetic field, produced either by a machine or in nature. This chapter does not go into detail about magnetometers.

Differences ...

Get Beginning PhoneGap now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.