📄 lbsmidlet.java
字号:
package com.myfistlbsapp;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.location.Coordinates;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.ProximityListener;
import javax.microedition.location.QualifiedCoordinates;
import javax.microedition.midlet.MIDlet;
public class LBSMIDlet extends MIDlet implements CommandListener, LocationListener, ProximityListener {
private Command exitCommand;
private Command startCommand;
private Command waypointCommand, tolocationCommand,towaypointCommand;
private Form locationForm,waypointForm,noAPIForm;
private int lat,lon,alt,speed,course,dist,state,proximity;
private int waylat,waylon,wayrad,wayalt;
private double wplat,wplon,wpalt,wprad;
private Display display;
// private static PositionTestApp instance;
private Coordinates oldCoordinates = null, curCoordinates = null;
int az = 0;
LocationProvider lp = null;
private float distance = 0;
public LBSMIDlet(){
locationForm = new Form("Location Details");
waypointForm = new Form("New Waypoint");
noAPIForm = new Form("No Location API");
exitCommand = new Command("Exit", Command.EXIT, 1);
startCommand = new Command("Start", Command.OK, 1);
waypointCommand = new Command("Create", Command.OK, 1);
tolocationCommand = new Command("Location",Command.EXIT,1);
towaypointCommand = new Command("New Waypoint",Command.OK,1);
lat = locationForm.append(new StringItem("Latitude ", ""));
lon = locationForm.append(new StringItem("Longitude ",""));
alt = locationForm.append(new StringItem("Altitude ",""));
speed = locationForm.append(new StringItem("Speed ",""));
course = locationForm.append(new StringItem("Course ",""));
dist = locationForm.append(new StringItem("Distance ",""));
state = locationForm.append(new StringItem("",""));
proximity = locationForm.append(new StringItem("",""));
waylat = waypointForm.append(new TextField("Latitude ","",9,TextField.DECIMAL));
waylon = waypointForm.append(new TextField("Longitude ","",9,TextField.DECIMAL));
wayalt = waypointForm.append(new TextField("Altitude (m) ","",9,TextField.DECIMAL));
wayrad = waypointForm.append(new TextField("Radius (m) ","",9,TextField.DECIMAL));
noAPIForm.append(new StringItem("This device does not support the JSR 179 Location API",""));
}
protected void startApp() {
display = Display.getDisplay(this);
waypointForm.addCommand(waypointCommand);
waypointForm.addCommand(tolocationCommand);
waypointForm.setCommandListener(this);
locationForm.addCommand(exitCommand);
locationForm.addCommand(startCommand);
locationForm.addCommand(towaypointCommand);
locationForm.setCommandListener(this);
noAPIForm.addCommand(exitCommand);
if (hasLocationAPI()){
createLocationProvider();
display.setCurrent(locationForm);
} else
display.setCurrent(noAPIForm);
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0) {
}
public void commandAction(Command command, Displayable displayable) {
if(command == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
else if(command == startCommand){
Thread getLocationThread = new Thread() {
public void run(){
setListener();
}
};
getLocationThread.start();
}
else if (command == towaypointCommand){
display.setCurrent(waypointForm);
}
else if (command == waypointCommand){
wplat = Double.valueOf(((
TextField)waypointForm.get(waylat)).getString()).doubleValue();
wplon = Double.valueOf(((
TextField)waypointForm.get(waylon)).getString()).doubleValue();
wpalt = Double.valueOf(((
TextField)waypointForm.get(wayalt)).getString()).doubleValue();
wprad = Double.valueOf(((
TextField)waypointForm.get(wayrad)).getString()).doubleValue();
final Coordinates waypoint = new Coordinates((float) wplat,(float) wplon,
(float) wpalt);
Thread getProximityThread = new Thread() {
public void run(){
setProximity(waypoint,wprad);
}
};
getProximityThread.start();
}
else if (command == tolocationCommand){
display.setCurrent(locationForm);
}
}
void createLocationProvider() {
if ( lp==null ) {
Criteria cr = new Criteria();
try {
lp = LocationProvider.getInstance(cr);
} catch (LocationException le) {
le.printStackTrace();
}
}
}
public void locationUpdated(final LocationProvider locProvider, final Location loc) {
Thread getLocationThread = new Thread(){
public void run(){
getLocation(locProvider,loc);
}
};
getLocationThread.start();
}
public void providerStateChanged(LocationProvider locProvider, int newstate) {
((StringItem)locationForm.get(state)).setLabel("Location Provider State Changed. Location Provider now ");
if (newstate == LocationProvider.AVAILABLE)
((StringItem)locationForm.get(state)).setText("AVAILABLE");
else if (newstate == LocationProvider.TEMPORARILY_UNAVAILABLE)
((StringItem)locationForm.get(state)).setText("TEMPORARILY_UNAVAILABLE");
else if (newstate == LocationProvider.OUT_OF_SERVICE)
((StringItem)locationForm.get(state)).setText("OUT_OF_SERVICE");
}
public void monitoringStateChanged(boolean arg0) {
((StringItem)locationForm.get(state)).setLabel("Monitoring State Changed. ");
if (arg0 == false)
((StringItem)locationForm.get(state)).setText("Not currently monitoring");
else if (arg0 == true)
((StringItem)locationForm.get(state)).setText("Currently Monitoring");
}
public void proximityEvent(final Coordinates coordinates, final Location loc) {
Thread getLocationThread = new Thread(){
public void run(){
getLocation(coordinates,loc);
}
};
getLocationThread.start();
}
public boolean hasLocationAPI(){
if (System.getProperty("microedition.location.version") == null) {
return false;
} else {
return true;
}
}
private void setListener(){
lp.setLocationListener(this,2,-1,-1);
}
private void setProximity(Coordinates waypoint, double wprad){
try {
LocationProvider.addProximityListener(this,waypoint,(float) wprad);
} catch (LocationException e) {
e.printStackTrace();
}
}
private void getLocation(LocationProvider locProvider, Location loc){
getLocation(loc);
((StringItem)locationForm.get(proximity)).setLabel("You are outside the " +
wprad + " meter radius of the defined zone");
}
private void getLocation(Coordinates c, Location loc){
getLocation(loc);
((StringItem)locationForm.get(proximity)).setLabel("You are " +
loc.getQualifiedCoordinates().distance(c) +
" meters from (lat/long/alt)" +
c.getLatitude()+"/"+c.getLongitude()+"/"+c.getAltitude());
setProximity(c,wprad);
}
private void getLocation(Location loc){
try {
QualifiedCoordinates c = loc.getQualifiedCoordinates();
if (null == oldCoordinates){
oldCoordinates = new Coordinates(c.getLatitude(),c.getLongitude(),c.getAltitude());
}
else{
distance += c.distance(oldCoordinates);
curCoordinates = new Coordinates(c.getLatitude(),c.getLongitude(),c.getAltitude());
az = (int)oldCoordinates.azimuthTo(curCoordinates);
oldCoordinates.setAltitude(c.getAltitude());
oldCoordinates.setLatitude(c.getLatitude());
oldCoordinates.setLongitude(c.getLongitude());
}
if (c != null){
((StringItem)locationForm.get(lat)).setText(String.valueOf(c.getLatitude()));
((StringItem)locationForm.get(lon)).setText(String.valueOf(c.getLongitude()));
((StringItem)locationForm.get(alt)).setText(String.valueOf(c.getAltitude())+" m");
((StringItem)locationForm.get(speed)).setText(String.valueOf(loc.getSpeed()/1000*3600)+" km/h");
((StringItem)locationForm.get(course)).setText(String.valueOf(az)+" "+findDirection(az));
((StringItem)locationForm.get(dist)).setText(String.valueOf(distance/1000)+" km");
}
} catch (SecurityException se) {
// The MIDlet is not authorized to retrieve location information
}
}
private String findDirection(int azimuth){
if ((azimuth >= 337 && azimuth <= 360) || (azimuth >= 0 && azimuth < 23))
return "N";
if (azimuth >= 23 && azimuth < 68)
return "NE";
if (azimuth >= 68 && azimuth < 113)
return "E";
if (azimuth >= 113 && azimuth < 158)
return "SE";
if (azimuth >= 158 && azimuth < 203)
return "S";
if (azimuth >= 203 && azimuth < 248)
return "SW";
if (azimuth >= 248 && azimuth < 293)
return "W";
if (azimuth >= 293 && azimuth < 337)
return "NW";
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -