如何等待Android GPS修复onLocationChanged

时间:2013-11-25 23:13:09

标签: java android gps

我的应用程序倒计时30秒,然后获取用户位置

问题是GPS没有等待足够长的时间来修复,我如何编程等待直到有GPS修复?

以下是与此相关的2位重要代码

private boolean initLocationManager() {

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        boolean isGPSEnabled = false;
        boolean isNetworkEnabled = false;
        // Define the criteria how to select the locatioin provider -> use

        // getting network status
        if (LocationInfo.isNormalGps()) {
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
            if (isGPSEnabled) {
                locationManager.removeUpdates(this);
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, 400, 1, this);
                Log.d("GPS Enabled", "GPS Enabled");
                if (locationManager != null) {
                    Location location = locationManager
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) {

                        onLocationChanged(location);

                    }
                }

            }
        }

这会调用我的onlocationchanged函数

public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        Locale current = getResources().getConfiguration().locale;
        NumberFormat df = NumberFormat.getInstance(current);
        df.setMaximumFractionDigits(1);
        if (location != null) {
            LocationInfo.setLat(location.getLatitude());
            LocationInfo.setLon(location.getLongitude());

            // converting accuracy meters to feet
            LocationInfo.setAccuracy((location.getAccuracy() * 3.28f));
            if (location.getAccuracy() < LocationInfo.getMinAccuracy()) {
                LocationInfo.setAccurate(true);
            } else {
                LocationInfo.setAccurate(false);
            }
            // converting speed and altitude meters to feet

            double altitude = (location.getAltitude() * 3.28);
            // LocationInfo.setAltitude(Double.parseDouble(df.format(altitude)));

            LocationInfo.setAltitude((int) altitude);
            // converting speed to MPH or KPH depending on setting

            // if isMPH false - it assumes its KPH and times it by 1.6

            float speed = (location.getSpeed() * 2.23f);

            LocationInfo.setSpeed((int) speed);

            // KPH are only shown if set and not transmitted
            if (isSpeedMph == false) {
                speed = speed * 1.6f;

            }

            // data always transmitted in mph regardless of what user has set
            // LocationInfo.setSpeedToShow(Float.parseFloat(df.format(speed)));
            LocationInfo.setSpeedToShow((int) speed);

        }

    }

我只是不知道如何让它等待GPS修复!!?

0 个答案:

没有答案