java me定位API的简单使用
java me的JSR-179(Location API)是用于定位的。很多手机并没有GPS,因此不能精准定位。但是GPS也有问题,比较费电。
很多手机可以借助运营商的网络和基站三角定位,来获得比较粗略的定位。
在nokia 6210ci上测试了一下jsr-179的支持情况。运行效果是:
输出纬度和经度。把这段内容复制到google map的文本框中,显示的位置和我办公地点相差不到1公里。
代码类似这样:
protected void startApp() throws MIDletStateChangeException {
if (System.getProperty("microedition.location.version") == null) {
displayMessage("对不起,您的手机不支持定位功能。");
} else {
try {
displayMessage(getLocationMessage());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}private String getLocationMessage() throws LocationException,
InterruptedException {
Criteria c = new Criteria();
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();
return new StringBuffer().append(qc.getLatitude()).append(",").append(
qc.getLongitude()).toString();
}private void displayMessage(String message) {
display = Display.getDisplay(this);
TextBox t = new TextBox("手机定位(纬度,经度)", message, 256, 0);
display.setCurrent(t);
}
在写完使用android Location API获取经纬度值后才发现,是地图不准,上面的坐标在用卫星图看,还是比较准的。
这篇文章上的评论的 RSS feed TrackBack URI