Example usage for android.location Location hasAccuracy

List of usage examples for android.location Location hasAccuracy

Introduction

In this page you can find the example usage for android.location Location hasAccuracy.

Prototype

public boolean hasAccuracy() 

Source Link

Document

True if this location has a horizontal accuracy.

Usage

From source file:de.ncoder.sensorsystem.android.logging.JSONUtils.java

private static Object wrapLocation(Location loc) {
    try {/*ww  w .  j a v a  2s  . c  om*/
        JSONObject json = new JSONObject();
        json.put("provider", loc.getProvider());
        json.put("latitude", loc.getLatitude());
        json.put("longitude", loc.getLongitude());
        if (loc.hasAccuracy())
            json.put("accuracy", loc.getAccuracy());
        json.put("time", loc.getTime());
        if (loc.hasAltitude())
            json.put("alt", loc.getAltitude());
        if (loc.hasSpeed())
            json.put("vel", loc.getSpeed());
        if (loc.hasBearing())
            json.put("bear", loc.getBearing());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
            if (loc.isFromMockProvider())
                json.put("mock", true);
        if (loc.getExtras() != null) {
            json.put("extra", wrap(loc.getExtras()));
        }
        return json;
    } catch (JSONException e) {
        return loc.toString() + " threw " + e.toString();
    }
}

From source file:uk.ac.horizon.ug.exploding.client.LocationUtils.java

public static void registerOnThread(Context context, LocationListener locationCallback, Listener listener) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getAllProviders();
    Log.i(TAG, "Found " + providers.size() + " location providers");
    for (String provider : providers) {
        if (locationManager.isProviderEnabled(provider)) {
            Log.i(TAG, "Provider " + provider + " enabled");
        } else {//from  w w  w  . j a v  a2 s .  c o  m
            Log.i(TAG, "Provider " + provider + " disabled");
        }
    }
    for (int pi = 0; pi < PROVIDERS.length; pi++) {
        String provider = PROVIDERS[pi];
        if (locationManager.isProviderEnabled(provider)) {
            Log.i(TAG, "Registering with provider " + provider);
            Location loc = locationManager.getLastKnownLocation(provider);
            if (loc != null) {
                Log.i(TAG,
                        "Last Location, provider=" + loc.getProvider() + ", lat=" + loc.getLatitude()
                                + ", long=" + loc.getLongitude() + ", bearing="
                                + (loc.hasBearing() ? "" + loc.getBearing() : "NA") + ", speed="
                                + (loc.hasSpeed() ? "" + loc.getSpeed() : "NA") + ", accuracy="
                                + (loc.hasAccuracy() ? "" + loc.getAccuracy() : "NA") + ", alt="
                                + (loc.hasAltitude() ? "" + loc.getAltitude() : "NA"));

                ZoneService.updateLocation(context, loc);

            }
            //if (!"passive".equals(provider))
            if (locationCallback != null)
                locationManager.requestLocationUpdates(provider, 0/*minTime*/, 0/*minDistance*/,
                        locationCallback);
        } else
            Log.e(TAG, "Required provider " + provider + " not enabled!");
    }
    if (listener != null)
        locationManager.addGpsStatusListener(listener);
}

From source file:org.smap.smapTask.android.receivers.LocationChangedReceiver.java

private boolean isAccurateLocation(Location location) {

    boolean accurate = true;
    if (!location.hasAccuracy() || location.getAccuracy() >= Constants.GPS_ACCURACY) {
        Log.d(TAG, "Ignore onLocationChangedAsync. Poor accuracy.");
        accurate = false;/*from ww w. ja v a 2  s.  co  m*/
    }
    return accurate;
}

From source file:org.runnerup.tracker.GpsStatus.java

@Override
public void onLocationChanged(Location location) {
    System.arraycopy(mHistory, 0, mHistory, 1, HIST_LEN - 1);
    mHistory[0] = location;// w  w  w  .j a v a 2  s.c  om
    if (location.hasAccuracy() && location.getAccuracy() < mFixAccurancy) {
        mIsFixed = true;
    } else if (mHistory[1] != null && (location.getTime() - mHistory[1].getTime()) <= (1000 * mFixTime)) {
        mIsFixed = true;
    } else if (mKnownSatellites >= mFixSatellites) {
        mIsFixed = true;
    }
    if (listener != null)
        listener.onTick();
}

From source file:co.edu.uniajc.vtf.ar.ARViewActivity.java

@Override
public void onLocationChanged(Location location) {
    this.coLastLocation = location;
    if (location.hasAccuracy()) {
        //if acccuracy is lower o equals than 40 call AR View
        if (location.getAccuracy() <= 40) {
            this.callARView();
        }//  ww w  .  j  a  v a2s  .c o  m
    }
}

From source file:can.yrt.onebusaway.ReportStopProblemFragment.java

@Override
protected ReportLoader createLoader(Bundle args) {
    String stopId = args.getString(STOP_ID);

    ObaReportProblemWithStopRequest.Builder builder = new ObaReportProblemWithStopRequest.Builder(getActivity(),
            stopId);/*from  w  ww . jav a 2 s. co  m*/

    // Code
    String code = SPINNER_TO_CODE[mCodeView.getSelectedItemPosition()];
    if (code != null) {
        builder.setCode(code);
    }

    // Comment
    CharSequence comment = mUserComment.getText();
    if (!TextUtils.isEmpty(comment)) {
        builder.setUserComment(comment.toString());
    }

    // Location / Location accuracy
    Location location = LocationHelp.getLocation2(getActivity());
    if (location != null) {
        builder.setUserLocation(location.getLatitude(), location.getLongitude());
        if (location.hasAccuracy()) {
            builder.setUserLocationAccuracy((int) location.getAccuracy());
        }
    }

    return new ReportLoader(getActivity(), builder.build());
}

From source file:com.joulespersecond.seattlebusbot.ReportStopProblemFragment.java

@Override
protected ReportLoader createLoader(Bundle args) {
    String stopId = args.getString(STOP_ID);

    ObaReportProblemWithStopRequest.Builder builder = new ObaReportProblemWithStopRequest.Builder(getActivity(),
            stopId);/*from ww  w  .  j  a  v a2  s.  c om*/

    // Code
    String code = SPINNER_TO_CODE[mCodeView.getSelectedItemPosition()];
    if (code != null) {
        builder.setCode(code);
    }

    // Comment
    CharSequence comment = mUserComment.getText();
    if (!TextUtils.isEmpty(comment)) {
        builder.setUserComment(comment.toString());
    }

    // Location / Location accuracy
    Location location = LocationHelp.getLocation2(getActivity(), mLocationClient);
    if (location != null) {
        builder.setUserLocation(location.getLatitude(), location.getLongitude());
        if (location.hasAccuracy()) {
            builder.setUserLocationAccuracy((int) location.getAccuracy());
        }
    }

    return new ReportLoader(getActivity(), builder.build());
}

From source file:com.vonglasow.michael.satstat.GpsSectionFragment.java

/**
 * Called by {@link MainActivity} when a new location is found by the GPS location provider.
 * Stores the location and updates GPS display and map view.
 *//*from  www .ja va 2s  .  c o  m*/
public void onLocationChanged(Location location) {
    if (location.hasAccuracy()) {
        Float getAcc = (float) 0.0;
        if (mainActivity.prefUnitType) {
            getAcc = (float) (location.getAccuracy());
        } else {
            getAcc = (float) (location.getAccuracy() * (float) 3.28084);
        }
        gpsAccuracy.setText(String.format("%.0f", getAcc));
        gpsAccuracyUnit
                .setText(getString(((mainActivity.prefUnitType) ? R.string.unit_meter : R.string.unit_feet)));
    } else {
        gpsAccuracy.setText(getString(R.string.value_none));
        gpsAccuracyUnit.setText("");
    }

    if (mainActivity.prefCoord == SettingsActivity.KEY_PREF_COORD_DECIMAL) {
        gpsCoordLayout.setVisibility(View.GONE);
        gpsLatLayout.setVisibility(View.VISIBLE);
        gpsLonLayout.setVisibility(View.VISIBLE);
        gpsLat.setText(String.format("%.5f%s", location.getLatitude(), getString(R.string.unit_degree)));
        gpsLon.setText(String.format("%.5f%s", location.getLongitude(), getString(R.string.unit_degree)));
    } else if (mainActivity.prefCoord == SettingsActivity.KEY_PREF_COORD_MIN) {
        gpsCoordLayout.setVisibility(View.GONE);
        gpsLatLayout.setVisibility(View.VISIBLE);
        gpsLonLayout.setVisibility(View.VISIBLE);
        double dec = location.getLatitude();
        double deg = (int) dec;
        double min = 60.0 * (dec - deg);
        gpsLat.setText(
                String.format("%.0f%s %.3f'", deg, getString(R.string.unit_degree), min + /*rounding*/ 0.0005));
        dec = location.getLongitude();
        deg = (int) dec;
        min = 60.0 * (dec - deg);
        gpsLon.setText(
                String.format("%.0f%s %.3f'", deg, getString(R.string.unit_degree), min + /*rounding*/ 0.0005));
    } else if (mainActivity.prefCoord == SettingsActivity.KEY_PREF_COORD_SEC) {
        gpsCoordLayout.setVisibility(View.GONE);
        gpsLatLayout.setVisibility(View.VISIBLE);
        gpsLonLayout.setVisibility(View.VISIBLE);
        double dec = location.getLatitude();
        double deg = (int) dec;
        double tmp = 60.0 * (dec - deg);
        double min = (int) tmp;
        double sec = 60.0 * (tmp - min);
        gpsLat.setText(String.format("%.0f%s %.0f' %.1f\"", deg, getString(R.string.unit_degree), min,
                sec + /*rounding*/ 0.05));
        dec = location.getLongitude();
        deg = (int) dec;
        tmp = 60.0 * (dec - deg);
        min = (int) tmp;
        sec = 60.0 * (tmp - min);
        gpsLon.setText(String.format("%.0f%s %.0f' %.1f\"", deg, getString(R.string.unit_degree), min,
                sec + /*rounding*/ 0.05));
    } else if (mainActivity.prefCoord == SettingsActivity.KEY_PREF_COORD_MGRS) {
        gpsLatLayout.setVisibility(View.GONE);
        gpsLonLayout.setVisibility(View.GONE);
        gpsCoordLayout.setVisibility(View.VISIBLE);
        gpsCoord.setText(new LatLng(location.getLatitude(), location.getLongitude()).toMGRSRef()
                .toString(MGRSRef.PRECISION_1M));
    }
    if (mainActivity.prefUtc)
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
    else
        df.setTimeZone(TimeZone.getDefault());
    gpsTime.setText(df.format(new Date(location.getTime())));

    if (location.hasAltitude()) {
        Float getAltitude = (float) 0.0;
        if (mainActivity.prefUnitType) {
            getAltitude = (float) (location.getAltitude());
        } else {
            getAltitude = (float) (location.getAltitude() * (float) 3.28084);
        }
        gpsAlt.setText(String.format("%.0f", getAltitude));
        gpsAltUnit.setText(getString(((mainActivity.prefUnitType) ? R.string.unit_meter : R.string.unit_feet)));
        orDeclination.setText(String.format("%.0f%s",
                new GeomagneticField((float) location.getLatitude(), (float) location.getLongitude(),
                        (float) (getAltitude), location.getTime()).getDeclination(),
                getString(R.string.unit_degree)));
    } else {
        gpsAlt.setText(getString(R.string.value_none));
        gpsAltUnit.setText("");
        orDeclination.setText(getString(R.string.value_none));
    }

    if (location.hasBearing()) {
        gpsBearing.setText(String.format("%.0f%s", location.getBearing(), getString(R.string.unit_degree)));
        gpsOrientation.setText(MainActivity.formatOrientation(this.getContext(), location.getBearing()));
    } else {
        gpsBearing.setText(getString(R.string.value_none));
        gpsOrientation.setText(getString(R.string.value_none));
    }

    if (location.hasSpeed()) {
        Float getSpeed = (float) 0.0;
        if (mainActivity.prefUnitType) {
            getSpeed = (float) (location.getSpeed() * 3.6f);
        } else {
            getSpeed = (float) (location.getSpeed() * 3.6f * 2.23694f);
        }
        gpsSpeed.setText(String.format("%.0f", getSpeed));
        gpsSpeedUnit.setText(getString(((mainActivity.prefUnitType) ? R.string.unit_km_h : R.string.unit_mph)));
    } else {
        gpsSpeed.setText(getString(R.string.value_none));
        gpsSpeedUnit.setText("");
    }

    // note: getting number of sats in fix by looking for "satellites"
    // in location's extras doesn't seem to work, always returns 0 sats
}

From source file:com.vonglasow.michael.satstat.ui.GpsSectionFragment.java

/**
 * Called by {@link MainActivity} when a new location is found by the GPS location provider.
 * Stores the location and updates GPS display and map view.
 *///from w ww .  j a  v  a2  s . co  m
public void onLocationChanged(Location location) {
    if (location.hasAccuracy()) {
        Float getAcc = (float) 0.0;
        if (mainActivity.prefUnitType) {
            getAcc = (float) (location.getAccuracy());
        } else {
            getAcc = (float) (location.getAccuracy() * (float) 3.28084);
        }
        gpsAccuracy.setText(String.format("%.0f", getAcc));
        gpsAccuracyUnit
                .setText(getString(((mainActivity.prefUnitType) ? R.string.unit_meter : R.string.unit_feet)));
    } else {
        gpsAccuracy.setText(getString(R.string.value_none));
        gpsAccuracyUnit.setText("");
    }

    if (mainActivity.prefCoord == Const.KEY_PREF_COORD_DECIMAL) {
        gpsCoordLayout.setVisibility(View.GONE);
        gpsLatLayout.setVisibility(View.VISIBLE);
        gpsLonLayout.setVisibility(View.VISIBLE);
        gpsLat.setText(String.format("%.5f%s", location.getLatitude(), getString(R.string.unit_degree)));
        gpsLon.setText(String.format("%.5f%s", location.getLongitude(), getString(R.string.unit_degree)));
    } else if (mainActivity.prefCoord == Const.KEY_PREF_COORD_MIN) {
        gpsCoordLayout.setVisibility(View.GONE);
        gpsLatLayout.setVisibility(View.VISIBLE);
        gpsLonLayout.setVisibility(View.VISIBLE);
        double dec = location.getLatitude();
        double deg = (int) dec;
        double min = 60.0 * (dec - deg);
        gpsLat.setText(
                String.format("%.0f%s %.3f'", deg, getString(R.string.unit_degree), min + /*rounding*/ 0.0005));
        dec = location.getLongitude();
        deg = (int) dec;
        min = 60.0 * (dec - deg);
        gpsLon.setText(
                String.format("%.0f%s %.3f'", deg, getString(R.string.unit_degree), min + /*rounding*/ 0.0005));
    } else if (mainActivity.prefCoord == Const.KEY_PREF_COORD_SEC) {
        gpsCoordLayout.setVisibility(View.GONE);
        gpsLatLayout.setVisibility(View.VISIBLE);
        gpsLonLayout.setVisibility(View.VISIBLE);
        double dec = location.getLatitude();
        double deg = (int) dec;
        double tmp = 60.0 * (dec - deg);
        double min = (int) tmp;
        double sec = 60.0 * (tmp - min);
        gpsLat.setText(String.format("%.0f%s %.0f' %.1f\"", deg, getString(R.string.unit_degree), min,
                sec + /*rounding*/ 0.05));
        dec = location.getLongitude();
        deg = (int) dec;
        tmp = 60.0 * (dec - deg);
        min = (int) tmp;
        sec = 60.0 * (tmp - min);
        gpsLon.setText(String.format("%.0f%s %.0f' %.1f\"", deg, getString(R.string.unit_degree), min,
                sec + /*rounding*/ 0.05));
    } else if (mainActivity.prefCoord == Const.KEY_PREF_COORD_MGRS) {
        gpsLatLayout.setVisibility(View.GONE);
        gpsLonLayout.setVisibility(View.GONE);
        gpsCoordLayout.setVisibility(View.VISIBLE);
        gpsCoord.setText(new LatLng(location.getLatitude(), location.getLongitude()).toMGRSRef()
                .toString(MGRSRef.PRECISION_1M));
    }
    if (mainActivity.prefUtc)
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
    else
        df.setTimeZone(TimeZone.getDefault());
    gpsTime.setText(df.format(new Date(location.getTime())));

    if (location.hasAltitude()) {
        Float getAltitude = (float) 0.0;
        if (mainActivity.prefUnitType) {
            getAltitude = (float) (location.getAltitude());
        } else {
            getAltitude = (float) (location.getAltitude() * (float) 3.28084);
        }
        gpsAlt.setText(String.format("%.0f", getAltitude));
        gpsAltUnit.setText(getString(((mainActivity.prefUnitType) ? R.string.unit_meter : R.string.unit_feet)));
        orDeclination.setText(String.format("%.0f%s",
                new GeomagneticField((float) location.getLatitude(), (float) location.getLongitude(),
                        (float) (getAltitude), location.getTime()).getDeclination(),
                getString(R.string.unit_degree)));
    } else {
        gpsAlt.setText(getString(R.string.value_none));
        gpsAltUnit.setText("");
        orDeclination.setText(getString(R.string.value_none));
    }

    if (location.hasBearing()) {
        gpsBearing.setText(String.format("%.0f%s", location.getBearing(), getString(R.string.unit_degree)));
        gpsOrientation.setText(MainActivity.formatOrientation(this.getContext(), location.getBearing()));
    } else {
        gpsBearing.setText(getString(R.string.value_none));
        gpsOrientation.setText(getString(R.string.value_none));
    }

    if (location.hasSpeed()) {
        Float getSpeed = (float) 0.0;
        if (mainActivity.prefKnots) {
            getSpeed = (float) (location.getSpeed() * 1.943844f);
        } else if (mainActivity.prefUnitType) {
            getSpeed = (float) (location.getSpeed() * 3.6f);
        } else {
            getSpeed = (float) (location.getSpeed() * 2.23694f);
        }
        gpsSpeed.setText(String.format("%.0f", getSpeed));
        gpsSpeedUnit.setText(getString(((mainActivity.prefKnots) ? R.string.unit_kn
                : (mainActivity.prefUnitType) ? R.string.unit_km_h : R.string.unit_mph)));
    } else {
        gpsSpeed.setText(getString(R.string.value_none));
        gpsSpeedUnit.setText("");
    }

    // note: getting number of sats in fix by looking for "satellites"
    // in location's extras doesn't seem to work, always returns 0 sats
}

From source file:org.onebusaway.android.report.ui.ReportStopProblemFragment.java

@Override
protected ReportLoader createLoader(Bundle args) {
    String stopId = args.getString(STOP_ID);

    ObaReportProblemWithStopRequest.Builder builder = new ObaReportProblemWithStopRequest.Builder(getActivity(),
            stopId);/*from   w  w  w.  ja  va2s .  com*/

    // Code
    String code = SPINNER_TO_CODE[mCodeView.getSelectedItemPosition()];
    if (code != null) {
        builder.setCode(code);
    }

    // Comment
    CharSequence comment = mUserComment.getText();
    if (!TextUtils.isEmpty(comment)) {
        builder.setUserComment(comment.toString());
    }

    // Location / Location accuracy
    Location location = Application.getLastKnownLocation(getActivity(), mGoogleApiClient);
    if (location != null) {
        builder.setUserLocation(location.getLatitude(), location.getLongitude());
        if (location.hasAccuracy()) {
            builder.setUserLocationAccuracy((int) location.getAccuracy());
        }
    }

    return new ReportLoader(getActivity(), builder.build());
}