Search code examples
blackberrygpsblackberry-storm

How to get current location in blackberry device?


In simulator my gps code work fine. But when I install my app in device I can't get current latitude and longitude.

When I send lat long from simulator it get proper lat long which is send through simulator. I don't know why is not working in device?

I have already enable from Option > Advance option > gps > gps servce and set Location ON. Is there any other setting for get current location in device?

private boolean currentLocation() {  
    boolean retval = true;  
   try {  
       LocationProvider lp = LocationProvider.getInstance(null);  
       if (lp != null) {  
            lp.setLocationListener(new LocationListenerImpl(), interval, 1, 1);  
        } else {  
            // GPS is not supported, that sucks!  
            // Here you may want to use UiApplication.getUiApplication() and post a Dialog box saying that it does not work  
            retval = false;  
        }  
    } catch (LocationException e) {  
        System.out.println("Error: " + e.toString());  
    }  

    return retval;  
}  

private class LocationListenerImpl implements LocationListener {  
    public void locationUpdated(LocationProvider provider, Location location) {  
        if (location.isValid()) {  
            heading = location.getCourse();  
            longitude = location.getQualifiedCoordinates().getLongitude();  
            latitude = location.getQualifiedCoordinates().getLatitude();  
            altitude = location.getQualifiedCoordinates().getAltitude();  
            speed = location.getSpeed();  

            // This is to get the Number of Satellites  
            String NMEA_MIME = "application/X-jsr179-location-nmea";  
            satCountStr = location.getExtraInfo("satellites");  
            if (satCountStr == null) {  
                satCountStr = location.getExtraInfo(NMEA_MIME);  
            }  

            // this is to get the accuracy of the GPS Cords  
            QualifiedCoordinates qc = location.getQualifiedCoordinates();  
            accuracy = qc.getHorizontalAccuracy();  
        }  
    }  

Solution

  • Try this code

    Thread thread = new Thread(new Runnable() {
            public void run() {
                bCriteria = new BlackBerryCriteria();
                if (GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_CELLSITE)) {
                    bCriteria.setMode(GPSInfo.GPS_MODE_CELLSITE);
                } else if (GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_ASSIST)) {
                    bCriteria.setMode(GPSInfo.GPS_MODE_ASSIST);
                } else if (GPSInfo
                        .isGPSModeAvailable(GPSInfo.GPS_MODE_AUTONOMOUS)) {
                    bCriteria.setMode(GPSInfo.GPS_MODE_AUTONOMOUS);
                } else {
                    bCriteria.setCostAllowed(true);
                    bCriteria
                            .setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
                }
    
                try {
                    bProvider = (BlackBerryLocationProvider) BlackBerryLocationProvider
                            .getInstance(bCriteria);
                    if (bProvider != null) {
                        bProvider.setLocationListener(new handleGPSListener(),
                                -1, -1, -1);
                        try {
                            bLocation = (BlackBerryLocation) bProvider
                                    .getLocation(60);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
    
                    }
                } catch (LocationException lex) {
    
                    lex.printStackTrace();
                    return;
                }
            }
        });
        thread.start();
    

    then implement Location Listener in the class

    public class handleGPSListener implements LocationListener {
        public void locationUpdated(LocationProvider provider, Location location) {
            if (location.isValid()) {
    
    
            }
        }
    
        public void providerStateChanged(LocationProvider provider, int newState) {
        }
    }