代码实例
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;
public class locationMidlet extends MIDlet implements CommandListener
{
Command Exit = new Command("Exit", Command.EXIT, 0);
public locationMidlet()
{
}
public void startApp()
{
Form f = new Form("请等等..");
f.append("正在获取位置信息");
f.addCommand(Exit);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
try
{
Criteria c = new Criteria();//Criteria在JSR179包里,解压后在javax\microedition\location
// c.setHorizontalAccuracy(1000);
// c.setVerticalAccuracy(1000);
// c.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
LocationProvider lp = LocationProvider.getInstance(c);
Location loc = lp.getLocation(60);
QualifiedCoordinates qc = loc.getQualifiedCoordinates();
AddressInfo adr = loc.getAddressInfo();
if(adr!=null)
System.out.println( adr.getField(AddressInfo.CITY));
f.append("Alt: " + qc.getAltitude());
f.append("Lat: " + qc.getLatitude());
f.append("Long: " + qc.getLongitude());
}
catch (Exception e)
{
f.append("Exception: " + e);
}
}
public void pauseApp()
{
}
public void destroyApp(boolean destroy)
{
}
public void commandAction(Command c, Displayable s)
{
if (c == Exit)
{
destroyApp(true);
notifyDestroyed();
}
}
}
实例2
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;
class MyCanvas extends Canvas
{
GISMIDlet GIS;
public MyCanvas(GISMIDlet GIS)
{
this.GIS = GIS;
}
private double lat;
private double lon;
public void setInfo(double lat, double lon)
{
this.lat = lat;
this.lon = lon;
}
protected void paint(Graphics g)
{
// TODO 自动产生方法 Stub
Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL);
g.setFont(f);
// use font height as a line height
int lineHeight = f.getHeight();
// current line counter
int line = 0;
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
g.drawString("Lat, Lon (" + GIS.formatDouble(lat, 5) + ", " + GIS.formatDouble(lon, 5) + ")", 0, lineHeight * (line++), Graphics.TOP | Graphics.LEFT);
}
}
public class GISMIDlet extends MIDlet implements CommandListener, LocationListener
{
private Command exitCmd = new Command("Exit", Command.EXIT, 1);
//private Form mainForm;
private MyCanvas myCanvas;
protected double lat; // 纬度
protected double lon; // 经度
//private LocationProvider locationProvider;
//private TextField latField; // 緯度欄位
//private TextField lonField; // 經度欄位
public GISMIDlet()
{
/* mainForm = new Form("GIS 經緯度測試");
latField = new TextField("緯度:", formatDouble(lat, 5), 15,
TextField.DECIMAL); // 設定緯度欄位格式
lonField = new TextField("經度:", formatDouble(lon, 5), 15,
TextField.DECIMAL); // 設定經度欄位格式
mainForm.append(latField); // 增加緯度欄位
mainForm.append(lonField); // 增加經度欄位
mainForm.addCommand(exitCmd); // 增加軟體按鈕
mainForm.setCommandListener(this); // 監聽按鈕事件 */
myCanvas = new MyCanvas(this);
myCanvas.addCommand(exitCmd);
myCanvas.setCommandListener(this);
Display.getDisplay(this).setCurrent(myCanvas); // 顯示畫面
}
protected void startApp() throws MIDletStateChangeException
{
Criteria c1 = new Criteria(); // 存取座標之限制
c1.setHorizontalAccuracy(500); // 水平
c1.setVerticalAccuracy(25);
/* try {
while (true) {
Thread.sleep(5000); // 每幾秒顯示一次資料 */
try
{
LocationProvider lp = LocationProvider.getInstance(c1); // 讀取座標之物件
if (lp != null)
{
lp.setLocationListener(this, -1, 0, 0); // 每幾秒讀取一次座標
}
Location LC = lp.getLocation(60);
Coordinates C = LC.getQualifiedCoordinates(); // 將座標封存
if (C != null)
{
lat = C.getLatitude(); // 緯度
lon = C.getLongitude(); // 經度
myCanvas.setInfo(lat, lon);
}
}
catch (LocationException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
// TODO 自動產生 catch 區塊
e.printStackTrace();
}
} // end method startApp
protected void pauseApp()
{
// TODO 自動產生方法 Stub
} // end method pauseApp
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
// TODO 自動產生方法 Stub
} // end method destroyApp
public void commandAction(Command c, Displayable arg1)
{
if (c == exitCmd)
{
try
{
destroyApp(false);
}
catch (MIDletStateChangeException exception)
{
System.out.println("MIDletStateChangeException");
}
notifyDestroyed();
}
} // end method commandAction
public String formatDouble(double value, int decimals)
{
String doubleStr = "" + value;
int index = doubleStr.indexOf(".") != -1 ? doubleStr.indexOf(".")
: doubleStr.indexOf(",");
// Decimal point can not be found...
if (index == -1)
{
return doubleStr;
}
// Truncate all decimals
if (decimals == 0)
{
return doubleStr.substring(0, index);
}
int len = index + decimals + 1;
if (len >= doubleStr.length())
{
len = doubleStr.length();
}
double d = Double.parseDouble(doubleStr.substring(0, len));
return String.valueOf(d);
} // end method formatDouble
public void locationUpdated(LocationProvider lp, final Location location)
{
new Thread()
{
public void run()
{
if (location != null && location.isValid())
{
Coordinates C = location.getQualifiedCoordinates();
if (C != null)
{
lat = C.getLatitude(); // 緯度
lon = C.getLongitude(); // 經度
myCanvas.setInfo(lat, lon);
myCanvas.repaint();
}
}
}
}.start();
}
public void providerStateChanged(LocationProvider arg0, int arg1)
{
// TODO 自動產生方法 Stub
}
/*public void updata(double lat, double lon) {
latField.setLabel("緯度:" + formatDouble(lat, 5)); // 更新緯度欄位
lonField.setLabel("經度:" + formatDouble(lon, 5)); // 更新經度欄位
latField.setString("" + formatDouble(lat, 5)); // 更新緯度欄位
lonField.setString("" + formatDouble(lon, 5)); // 更新經度欄位
} // end method updata*/
} // end class GISMIDlet