GPS location information issue

255 views Asked by At

In my application,there is a need of take current location information, I use the follwing code; and call it buttonclick,But it doesn't work in the device and in the simulator,if any one have any idea please help.

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

There are 4 answers

0
Michael Donohue On

Step one is to put some logging in your exception handlers.

This will probably show you some exception is being thrown. Once you have that figured out, you can see how the code might be doing something wrong that causes the exception.

0
Richard On

Have you simulated a GPS in the simulator? Simulate -> GPS Position then in the dialog add a position or configure a simulated route.

0
Eric Giguere On

This is probably not a GPS problem. It's likely that you're trying to update a UI field without being on the event thread. See my answer to this related question for information on how to avoid that.

1
Nequita On
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());
}

for an explanation to this "Mess" please see : blackberry location-based services