private void retrieveLocation() {
      Location location = null;
      final Iterator<String> providers =
          mLocationManager.getProviders(new Criteria(), true).iterator();
      while (providers.hasNext()) {
        final Location lastKnownLocation = mLocationManager.getLastKnownLocation(providers.next());
        // pick the most recent location
        if (location == null
            || (lastKnownLocation != null
                && location.getElapsedRealtimeNanos()
                    < lastKnownLocation.getElapsedRealtimeNanos())) {
          location = lastKnownLocation;
        }
      }

      // In the case there is no location available (e.g. GPS fix or network location
      // is not available yet), the longitude of the location is estimated using the timezone,
      // latitude and accuracy are set to get a good average.
      if (location == null) {
        Time currentTime = new Time();
        currentTime.set(System.currentTimeMillis());
        double lngOffset =
            FACTOR_GMT_OFFSET_LONGITUDE * (currentTime.gmtoff - (currentTime.isDst > 0 ? 3600 : 0));
        location = new Location("fake");
        location.setLongitude(lngOffset);
        location.setLatitude(0);
        location.setAccuracy(417000.0f);
        location.setTime(System.currentTimeMillis());
        location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());

        if (DEBUG) {
          Slog.d(TAG, "Estimated location from timezone: " + location);
        }
      }

      setLocation(location);
    }
  // The user has moved if the accuracy circles of the two locations don't overlap.
  private static boolean hasMoved(Location from, Location to) {
    if (to == null) {
      return false;
    }

    if (from == null) {
      return true;
    }

    // if new location is older than the current one, the device hasn't moved.
    if (to.getElapsedRealtimeNanos() < from.getElapsedRealtimeNanos()) {
      return false;
    }

    // Get the distance between the two points.
    float distance = from.distanceTo(to);

    // Get the total accuracy radius for both locations.
    float totalAccuracy = from.getAccuracy() + to.getAccuracy();

    // If the distance is greater than the combined accuracy of the two
    // points then they can't overlap and hence the user has moved.
    return distance >= totalAccuracy;
  }
Exemple #3
0
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
  public static JSONObject location2Json(Location location) throws JSONException {
    JSONObject latLng = new JSONObject();
    latLng.put("lat", location.getLatitude());
    latLng.put("lng", location.getLongitude());

    JSONObject params = new JSONObject();
    params.put("latLng", latLng);

    if (VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      params.put("elapsedRealtimeNanos", location.getElapsedRealtimeNanos());
    } else {
      params.put("elapsedRealtimeNanos", 0);
    }
    params.put("time", location.getTime());
    /*
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date date = new Date(location.getTime());
    params.put("timeFormatted", format.format(date));
    */
    if (location.hasAccuracy()) {
      params.put("accuracy", location.getAccuracy());
    }
    if (location.hasBearing()) {
      params.put("bearing", location.getBearing());
    }
    if (location.hasAltitude()) {
      params.put("altitude", location.getAltitude());
    }
    if (location.hasSpeed()) {
      params.put("speed", location.getSpeed());
    }
    params.put("provider", location.getProvider());
    params.put("hashCode", location.hashCode());
    return params;
  }