18.4 Enhancing the Driving Directions ApplicationThe driving directions application we demonstrated above is not particularly easy to use. For example, if we are lost while driving around, we need to find the route from the current location to the destination. To find out the current street address and manually enter it into the device could be a major nuisance. A driving directions application that is aware of the current location context and automatically fills out the From field can be much more valuable to users. In fact, most LBS business models require access to the mobile client's current location. 18.4.1 Location Determination TechniquesThere are several ways to obtain the current locations of mobile devices. Terminal-based: A GPS-equipped device can calculate its coordinates using GPS satellite signals. This method is accurate and straightforward, and it works everywhere in the world. However, GPS modules are expensive, slow, and a drain on batteries. Network-based: Cellular network operators can determine location of any phone in the network using its signal strength received by three nearby access stations (triangling). On-device smart applications (e.g., the driving directions application) can access the location data via the data network. With user authorization, the location data can also be made available to third parties from the carrier's Web site. This is an excellent approach if we need to track a large number of users from the back end (e.g., push services). However, the drawbacks are that the user must be within the cellular network coverage; the service is not yet available nationally, and the location accuracy is not as good as the GPS approach. Network-assisted GPS: Of course, we can combine the above two approaches: GPS devices can use network data to determine an approximate position first and then use the GPS module to get accurate corrections. This speeds up the GPS look-up process considerably. Local wireless network-based: Advances in new technologies will also enable location determination in local wireless networks, such as WiFi and Bluetooth networks, in the future. User-assisted: As the last resort, we can also ask the user to identify the closest landmark and calculate an approximate coordinate based on the known coordinate of the landmark. This works well in controlled environments like a college or company campus. NoteThe Oracle wireless application server works with wireless carrier location servers to provide SOAP interfaces for network-based location data of all devices in the network.
18.4.2 The Location API for J2METhe J2ME Location API (JSR 179) is a standard Optional Package for both CDC-based and CLDC-based devices. It allows J2ME applications to access location information on any device through standard APIs regardless of the underlying technology and service provider. Table 18.2 shows the entire J2ME Location API defined in the javax.microedition.location package. It is based on the v1.0 final release (September 2003).
NoteCLDC v1.1 is required for the J2ME Location API because coordinates are expressed in float or double numbers. Listing 18.3 illustrates how to construct a simple location-based application using the JSR 179 API. For more information about the J2ME Location API and usage examples, please refer to JSR 179 documentation. Listing 18.3. A simple J2ME Location API applicationCriteria criteria = new Criteria (); criteria.setPreferredResponseTime(20); criteria.seVerticalAccuracy(10); // set other criteria LocationProvider provider = LocationProvider.getInstance(criteria); // The StepTracker's locationUpdated() method // will be called every 1 second until the 100th second. // The passed location data cannot be more than 2 seconds old. StepTracker tracker = new StepTracker (); provider.setLocationListener(tracker, 1, 100, 2); // Add the collision handling logic. The CollisionHandler's // proximityEvent() method is called when the device // enters the 0.5 meter radius of either coord1 or coord2 Coordinates coord1 = new Coordinates(lat1, long1, alt1); Coordinates coord2 = new Coordinates(lat2, long2, alt2); CollisionHandler collision = new CollisionHandler (); provider.addProximityListener(collision, coord1, 0.5); provider.addProximityListener(collision, coord2, 0.5); public StepTracker implements LocationListener { public StepTracker () { } // Both threads below must return immediately. // So, put long processes in a separate thread. public void locationUpdated (LocationProvider provider, Location location) { // Do something with the new location // For example, update the steps on a map. } public void providerStateChanged(LocationProvider provider, int newState) { // Handle the state change. For example, if the // provider becomes unavailable, alert the user. } } public CollisionHandler implements ProximityListener { public CollisionHandler () { } public void proximityEvent(Coordinates coordinates, Location location) { // Handle the collision here. // For example, alert the user and provide a direction // to move away from the collision point. } public void monitoringStateChanged(boolean isActive) { // Handle the state change in the provider } } |