-1

在我的应用程序中,需要获取当前位置信息,我使用以下代码;并称之为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();
}
4

4 回答 4

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

于 2012-10-05T07:07:54.753 回答
0

你有在模拟器中模拟过 GPS 吗?模拟 -> GPS 位置,然后在对话框中添加位置或配置模拟路线。

于 2011-02-08T16:49:35.770 回答
0

第一步是在您的异常处理程序中添加一些日志记录。

这可能会告诉你一些异常被抛出。一旦你弄清楚了,你就可以看到代码是如何做错事而导致异常的。

于 2011-02-08T17:13:23.143 回答
0

这可能不是 GPS 问题。您可能正在尝试更新 UI 字段而不是在事件线程上。有关如何避免这种情况的信息,请参阅我对这个相关问题的回答。

于 2011-02-08T18:38:35.340 回答