Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
143023864X_HT5.pdf
Скачиваний:
8
Добавлен:
21.02.2016
Размер:
7.98 Mб
Скачать

C H A P T E R 5

Using the Geolocation API

Let’s say you want to create a web application that offers discounts and special deals on running shoes in stores that your application’s users are within walking (or running) distance away from. Using the Geolocation API, you can request users to share their location and, if they agree, you can provide them with instructions on how to get to a nearby store to pick up a new pair of shoes at a discounted rate.

Another example of the use of Geolocation could be an application that tracks how far you have run (or walked). You can picture using an application in a browser on a mobile phone that you turn on when you start a run. While you’re on the move, the application tracks how far you have run. The coordinates for the run can even be overlaid on a map, perhaps even with an elevation profile. If you’re running a race against other competitors, the application might even show your opponents’ locations.

Other Geolocation application ideas could be turn-by-turn GPS-style navigation, social networking applications that allow you to see exactly where your friends are, so you can pick the coffee shop you want to visit, and many more unusual applications.

In this chapter, we’ll explore what you can do with Geolocation—an exciting API that allows users to share their location with web applications so that they can enjoy location-aware services. First, we'll take a look at the source of Geolocation location informationthe latitude, longitude and other attributesand where they can come from (GPS, Wi-Fi, cellular triangulation, and so on). Then, we'll discuss the privacy concerns around using Geolocation data and how browsers work with this data.

After that, we’ll dive into a practical discussion about the two different position request functions (methods) within the Geolocation API: the one-shot position request and repeated position updates, and we'll show you how and when to use them. Next, we'll show you how to build a practical Geolocation application using the same API, and we'll finish up with a discussion about a few additional use cases and tips.

About Location Information

Using the Geolocation API is fairly straightforward. You request a position and, if the user agrees, the browser returns location information. The position is provided to the browser by the underlying device (for example, a laptop or a mobile phone) on which the Geolocation–enabled browser is running. The location information is provided as a set of latitude and longitude coordinates along with additional metadata. Armed with this location information, you can then build a compelling, location-aware application.

Latitude and Longitude Coordinates

The location information consists primarily of a pair of latitude and longitude coordinates like the ones shown in the following example, which shows the coordinates for beautiful Tahoe City, located on the shore of Lake Tahoe, America’s most beautiful mountain lake:

107

CHAPTER 5 USING THE GEOLOCATION API

Latitude: 39.17222, Longitude: -120.13778

In the preceding example, the latitude (the numerical value indicating distance north or south of the equator is 39.17222) and the longitude (the numerical value indicating distance east or west of Greenwich, England) is -120.13778.

Latitude and longitude coordinates can be expressed in different ways:

Decimal format (for example, 39.17222)

Degree Minute Second (DMS) format (for example, 39° 10' 20')

Note When you use the Geolocation API, coordinates are always returned in the decimal format.

In addition to latitude and longitude coordinates, Geolocation always provides the accuracy of the location coordinates. Additional metadata may also be provided, depending on the device that your browser is running on. These include altitude, altitudeAccuracy, heading, and speed. If this additional metadata is not available it will be returned as a null value.

Where Does Location Information Come From?

The Geolocation API does not specify which underlying technology a device has to use to locate the application's user. Instead, it simply exposes an API for retrieving location information. What is exposed, however, is the level of accuracy with which the location was pinpointed. There is no guarantee that the device's actual location returns an accurate location.

Location, Location

Peter says: “Here is a funny example of that. At home, I use a wireless network. I opened the Geolocation example application shown in this chapter in Firefox and it figured out that I was in Sacramento (about 75 miles from my actual physical location). Wrong, but not too surprising, because my Internet Service Provider is located in downtown Sacramento.

Then, I asked my sons, Sean and Rocky, to browse to the same page on their iPhones (using the same WiFi network). In Safari, it looked like they were located in Marysville, California—a town that is located 30 miles from Sacramento. Go figure!”

108

CHAPTER 5 USING THE GEOLOCATION API

A device can use any of the following sources:

IP address

Coordinate triangulation

Global Positioning System (GPS)

Wi-Fi with MAC addresses from RFID, Wi-Fi, and Bluetooth

GSM or CDMA cell phone IDs

User defined

Many devices use a combination of one or more sources to ensure an even higher accuracy. Each of these methods has its own pros and cons, as explained in the next sections.

IP Address Geolocation Data

In the past, IP address–based geolocation was the only way to get a possible location, but the returned locations often proved unreliable. IP address–based geolocation works by automatically looking up a user’s IP address and then retrieving the registrant's physical address. Therefore, if you have an ISP that provides you with an IP address, your location is often resolved to the physical address of your service provider that could be miles away. Table 5-1 shows the pros and cons of IP address–based geolocation data.

Table 5-1. Pros and Cons of IP Address–based Geolocation Data

Pros

Cons

Available everywhere

Not very accurate (wrong many times,

 

but also accurate only to the city level)

Processed on the server side

Can be a costly operation

 

 

Many websites advertise based on IP address locations. You can see this in action when you travel to another country and suddenly see advertisements for local services (based on the IP address of the country or region you are visiting).

GPS Geolocation Data

As long as you can see the sky, GPS can provide very accurate location results. A GPS fix is acquired by acquiring the signal from multiple GPS satellites that fly around the earth. However, it can take awhile to get a fix, which does not lend itself particularly well for applications that must start up rapidly.

Because it can take a long time to get a GPS location fix, you might want to query for the user’s location asynchronously. To show your application’s users that a fix is being acquired, you can add a status bar. Table 5-2 shows the pros and cons of GPS–based geolocation data.

109

CHAPTER 5 USING THE GEOLOCATION API

Table 5-2. Pros and Cons of GPS–based Geolocation Data

Pros

Cons

Very accurate

It can take a long time getting a location

 

fix, which can drain a user’s device’s

 

batteries

 

Does not work well indoors

 

May require additional hardware

 

 

Wi-Fi Geolocation Data

Wi-Fi–based geolocation information is acquired by triangulating the location based on the user's distance from a number of known Wi-Fi access points, mostly in urban areas. Unlike GPS, Wi-Fi is very accurate indoors as well as in urban areas. Table 5-3 shows the pros and cons of Wi-Fi–based geolocation data.

Table 5-3. Pros and Cons of Wi-Fi–based Geolocation Data

Pros

Cons

Accurate

Not good in rural areas with few wireless

 

access points

Works indoors

 

Can get fix quickly and cheaply

 

 

 

Cell Phone Geolocation Data

Cell phone–based geolocation information is acquired by triangulating the location based on the user's distance from a number of cell phone towers. This method provides a general location result that is fairly accurate. This method is often used in combination with Wi-Fi– and GPS–based geolocation information. Table 5-4 shows the pros and cons of cell phone–based geolocation data.

110