Info on the JSR 179 - Location API

02/05/06 Permalink

The MIDP Location API (JSR 179) enables developers to query network providers for information on locations. There was the original specification on the 9th September, 2003 and another one has recently been released on the 24th of March, 2006.

There aren't many phones out which support this set of classes. Michael Glenn has found some including the Nokia N91, Nokia 6265, Nokia 6265i, Motorola i605 (gps), i830 (gps), and Siemens CX65, C65, S6V, S65. I've found some more including Siemens CX75, C75, C70, C72, SXG75, C6V, M75.

I've written a very simple MIDlet that should display your coordinates, although I can't test it because the Emulator doesn't allow Location calls, and my Siemens C65 which does support the Location API doesn't seem to allow external access. I gave O2 a ring and it cost me £1 for them to tell me that they can't help. Brilliant. Anyway here's the code.


import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.location.*;

public class Location extends MIDlet
{
    String error = "";   	
    private static Display display = null;


    protected void startApp() throws MIDletStateChangeException
    {
                display = Display.getDisplay(this);
		System.out.println("loading");
		
		LocationProvider lp=null;
		javax.microedition.location.Location location=null;
		
		try
		{
		  lp= LocationProvider.getInstance(null);
		}
		catch(LocationException e)
		{
		  addError(e);	
		}
				
		try
		{
		                           //timeout 
                 location = lp.getLocation(20);
		}
		  catch(LocationException e)
		{
		  addError(e);		
		}
		  catch(InterruptedException e)
		{
		  addError(e);		
		}

	        String res="[RESULTS]\n";
		try{
			
			Coordinates coordinates = location.getQualifiedCoordinates();
			
			res+="Altitude:"+coordinates.getAltitude()+"\n";
			res+="Latitude:"+coordinates.getLatitude()+"\n";
			res+="Longitude:"+coordinates.getLongitude()+"\n";
			
		}catch(Exception e){
		addError(e);		
		}
		
		
		Form f = new Form("Results");
		
		f.append(res);
		f.append(error);
		
		display.setCurrent(f);
		
        
    }

    void addError(Exception e){
		e.printStackTrace();
		error+=e.getMessage()+"\n";
		
    }

    protected void pauseApp()
    {
    }

    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException
    {	
    }

    
    public static Display getDisplay()
    {
        return display;
    }

    
    	
} 

_____________________________________________________________________________________
Main stuff from Sun's Real-Life Use of JSR 179 (registration needed). 
Apologies if it's a bit scruffy - it's a cut and paste job from the pdf.

Carrier Methods
• Cell
• GPS/assisted-GPS
• Time offset/TOA
• Short-range (Bluetooth™, WLAN)
Usage depends on your phone, and your network.

LocationProvider
javax.microedition.location.LocationProvider
• Starting point for location request
• Specify criteria to get proper provider
getInstance(Criteria criteria)
• One-shot fix based on criteria
getLocation(int timeout)
• Setup a LocationListener to get updates
setLocationListener(LocationListener l, int interval, int
timeout, int maxAge)

Criteria
javax.microedition.location.Criteria
• Specifies the best suited LocationProvider
for your needs
• Knobs to turn
• Cost, speed and course, horizontal/vertical accuracy,
power consumption, response time, alt, address
• Note criteria unique among implementations
• Different quality-of-fix services may incur cost
• Address may not be available based on network
and back-end systems

Criteria Code Sample
Criteria cr = new Criteria();
cr.setCostAllowed(true); //default value
cr.setSpeedAndCourseRequired(true);
cr.setHorizontalAccuracy(500);
cr.setAltitudeRequired(true);
lp = LocationProvider.getInstance(cr);

Location
javax.microedition.location.Location
• Response for both positive and negative attempts
Location.isValid()
• Location source (CELLID, SATELLITE, TOA)
Location.getLocationMethod()
• Retrieve coordinates, speed, course, and time
• Getting formatted data (NMEA,LIF) and detailed
error description
Location.getExtraInfo(String mimetype)
getExtraInfo(“application/X-jsr179-location-nmea”)
getExtraInfo(“text/plain”)

Location Code Sample
if (loc.isValid()) {
if((loc.getLocationMethod() & Location.MTE_SATELLITE) !=0){
…println(“Method = Satellite”);
}
..println("Course="+loc.getCourse());
..println("Speed ="+loc.getSpeed());
qc = loc.getQualifiedCoordinates();
if (qc!=null) {
..println("Latitude ="+qc.getLatitude());
..println("Longitude="+qc.getLongitude());
..println("Altitude ="+qc.getAltitude());
}
}e
lse{
..println(loc.getExtraInfo("text/plain"));
}

LocationListener
javax.microedition.location.LocationListener
• Receives updates from a LocationProvider
setLocationListener(listener, interval,timeout,maxAge);
• interval: how often you want fix updates, in seconds
• timeout: how late update can be from interval, in seconds
• maxAge: acceptable age for cached info to be returned
• Best effort is giving to return in specified interval,
but should not be considered a timer
• Updates should be considered a trigger, processing

LocationListener Code Sample
class NavigationHelm implements LocationListener
{ void locationUpdated(LocationProvider p, Location l)
{
// call another thread to process
handler.handleUpdate(l);
}
void providerStateChanged(LocationProvider p, int state)
{
// not common for state to change
}
}
should be handled in separate thread

Coordinates
javax.microedition.location.Coordinates
• Holds Longitude, Latitude and Altitude
• Distance to another coordinate
Coordinates.distance(Coordinates to)
• Direction(azimuth) to another point
Coordinates.animuthTo(Coordinates to)
• Convert a coordinate to/from String
• QualifiedCoordinates adds accuracy


Coordinates Code Sample
// get your coordinates
qcMe = location.getQualifiedCoordinates();
// how far is destination
float distAway = qcMe.distance(qcDest);
// direction to dest(int to shorten example)
int dirTo = new FLoat(qcMe.azimuthTo(qcDest)).intValue();
if(dirTo >= 338 || dirTo <= 22)
..println("NORTH " + distAway + " meters";
if(dirTo >= 23 && dirTo <= 67)
..println("NORTH-EAST " + distAway + " meters";
if(dirTo >= 68 && dirTo <= 112)
..println("EAST " + distAway + " meters";

ProximityListener
javax.microedition.location.ProximityListener
• Receives updates bases on terminal crossing
into a defined radius around a coordinate
• Listener is notified only once, must re-register
to be notified in the future
• Report coordinates registered for and location
of terminal when crossing the radius

Landmark
javax.microedition.location.Landmark
• Links a known location with an associated name
• Contains name, description, QualifiedCoordinates,
and optional address
• If terminal doesn’t natively support addressing
coordinates could be reverse geo-coded using
a GIS and network lookups
• Landmark could be the response from searching
an exposed Web service

LandmarkStore
javax.microedition.location.LandmarkStore
• Shared persistent area to store, modify,
and delete landmarks
• Implementations may only have one store,
may not allow creating or deleting
• Landmarks can exist in multiple categories
• Retrieve landmarks by category and/or
geo-fence area

LandmarkStore Usage Considerations
• Store personal addresses and tracking
info to quickly recall
• Pros
• No code to write for persistence
• Landmarks can be utilized in other applications
saving duplicate data
• Enables modular applications, e.g., one application finds
landmark while another gives directions
• Cons
• Implementations may limit number of Landmarks
• Data is not secure, open to other applications
• Data could be altered/deleted by outside applications
getLandmarks(category,minLat,maxLat,minLong,maxLong)

Useful:

Share It: Digg | del.icio.us | Furl | reddit | Facebook | Yahoo! | Send to Phone

mobile-utopia.com | Feedback