2

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();  
        }  
    }  
Hitarth
  • 1,950
  • 3
  • 27
  • 52
  • What type of location provider do you use (autonomous, cellsite, assisted)? – Vit Khudenko Sep 25 '11 at 11:08
  • i have Storm 2 and i m using wi-fi . i am using LocationProvider class – Hitarth Sep 26 '11 at 07:04
  • you did not answer my question... Probably just post your code. – Vit Khudenko Sep 26 '11 at 07:11
  • maybe you are testing it in your office where of course there is no gps signal; do you get your position with bb maps? – Gabor Sep 26 '11 at 09:58
  • ya i m testing in office . how can i test it proper work or not ? – Hitarth Sep 26 '11 at 10:05
  • You need to go outdoors and make sure the GPS works with some other app (e.g. google maps), then you can try your app. I believe if other app GPS app works, then your app should also work OK. – Vit Khudenko Sep 26 '11 at 15:44
  • You pass `null` as `Criteria` when you call for `LocationProvider` instance. As the docs say in this case the default provider is requested. So with the current code you have no control over the location provider type. From my experience in this case you'll most likely get an autonomous location provider which requires satellite visibility (works only outdoors) and requires some time (1-2 minute) to get the first fix. – Vit Khudenko Sep 26 '11 at 15:50
  • ok thanx i will check other app and than my app. and than try again . – Hitarth Sep 26 '11 at 15:58

1 Answers1

1

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) {
    }
}
silwar
  • 6,470
  • 3
  • 46
  • 66