如何获取黑莓设备的当前位置?

时间:2011-09-25 07:12:11

标签: blackberry gps blackberry-storm

在模拟器中我的gps代码工作正常。但是当我在设备中安装我的应用程序时,我无法获得当前的经度和纬度。

当我从模拟器发送lat long时,它会通过模拟器发送到正确的lat长度。我不知道为什么不在设备上工作?

我已经从Option > Advance option > gps > gps servce启用并将位置设置为ON。 在设备中获取当前位置是否还有其他设置?

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

1 个答案:

答案 0 :(得分:1)

试试此代码

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();

然后在类

中实现Location Listener
public class handleGPSListener implements LocationListener {
    public void locationUpdated(LocationProvider provider, Location location) {
        if (location.isValid()) {


        }
    }

    public void providerStateChanged(LocationProvider provider, int newState) {
    }
}
相关问题