在我的应用程序中,需要获取当前位置信息,我使用以下代码;并将其称为buttonclick,但它在设备和模拟器中不起作用,如果有任何想法请帮助。
private void getLocationServices(){
Thread geoThread=new Thread(){
public void run(){
try{
Criteria myCriteria = new Criteria();
myCriteria.setCostAllowed(false);
try{
LocationProvider myLocationProvider = LocationProvider.getInstance(myCriteria);
try{
locationaddress=new AddressInfo();
Location myLocation = myLocationProvider.getLocation(300);
_lattitude = myLocation.getQualifiedCoordinates().getLatitude();
_longitude = myLocation.getQualifiedCoordinates().getLongitude();
int intLatitude = (int) _lattitude * 100000;
int intLongitude = (int) _longitude * 100000;
try{
Landmark[] results= Locator.reverseGeocode(intLatitude, intLongitude, Locator.ADDRESS);
if (results != null && results.length > 0) {
locationaddress=results[0].getAddressInfo();
lblLoc.setText(locationaddress.getField(AddressInfo.CITY));
}
}catch (Exception e) {
// TODO: handle exception
}
}catch (Exception e) {
// TODO: handle exception
}
}catch (Exception e) {
// TODO: handle exception
}
}catch (Exception e) {
// TODO: handle exception
}
}
};
geoThread.start();
}
答案 0 :(得分:0)
您是否在模拟器中模拟了GPS?模拟 - > GPS位置然后在对话框中添加位置或配置模拟路线。
答案 1 :(得分:0)
第一步是在你的异常处理程序中添加一些日志记录。
这可能会告诉你一些异常被抛出。一旦弄明白,你就可以看到代码可能导致错误导致异常。
答案 2 :(得分:0)
这可能不是GPS问题。您可能正在尝试更新UI字段而不在事件线程上。有关如何避免这种情况的信息,请参阅my answer to this related question。
答案 3 :(得分:0)
CustomMapField mMapField;
Coordinates mCoordinates;
BlackBerryCriteria blackBerryCriteria = null;
BlackBerryLocation blackBerryLocation = null;
BlackBerryLocationProvider blackBerryLocationProvider = null;
double Doublelat = 0.0;
double Doublelng = 0.0;
blackBerryCriteria = new BlackBerryCriteria();
if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_CELLSITE)){
blackBerryCriteria.setMode(GPSInfo.GPS_MODE_CELLSITE);
}else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_ASSIST)){
blackBerryCriteria.setMode(GPSInfo.GPS_MODE_ASSIST);
}else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_AUTONOMOUS)){
blackBerryCriteria.setMode(GPSInfo.GPS_MODE_AUTONOMOUS);
}else{
blackBerryCriteria.setCostAllowed(true);
blackBerryCriteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
} try {
blackBerryLocationProvider = (BlackBerryLocationProvider) BlackBerryLocationProvider.getInstance(blackBerryCriteria);
blackBerryLocation = (BlackBerryLocation) blackBerryLocationProvider.getLocation(60);
QualifiedCoordinates qualifiedCoordinates = blackBerryLocation.getQualifiedCoordinates();
Doublelat = qualifiedCoordinates.getLatitude();
Doublelng = qualifiedCoordinates.getLongitude();
mCoordinates = new Coordinates(Doublelat, Doublelng, 0);
MapView mapView = new MapView();
mapView.setLatitude(finalintlat);
mapView.setLongitude(finalintlng);
mapView.setZoom(10);
MapsArguments mapsArgs = new MapsArguments(mapView);
Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, mapsArgs);
}catch(Exception e){
System.out.println("Error in location :"+e.toString());
System.out.println("Error in location :"+e.getMessage());
}
有关此“混乱”的解释,请参阅:blackberry location-based services