List of usage examples for android.location Location distanceTo
public float distanceTo(Location dest)
From source file:br.com.bioscada.apps.biotracks.services.TrackRecordingService.java
/** * Called when location changed.// w ww. java2 s.c om * * @param location the location */ private void onLocationChangedAsync(Location location) { try { if (!isRecording() || isPaused()) { Log.w(TAG, "Ignore onLocationChangedAsync. Not recording or paused."); return; } Track track = myTracksProviderUtils.getTrack(recordingTrackId); if (track == null) { Log.w(TAG, "Ignore onLocationChangedAsync. No track."); return; } if (!LocationUtils.isValidLocation(location)) { Log.w(TAG, "Ignore onLocationChangedAsync. location is invalid."); return; } if (!location.hasAccuracy() || location.getAccuracy() >= recordingGpsAccuracy) { Log.d(TAG, "Ignore onLocationChangedAsync. Poor accuracy."); return; } // Fix for phones that do not set the time field if (location.getTime() == 0L) { location.setTime(System.currentTimeMillis()); } Location lastValidTrackPoint = getLastValidTrackPointInCurrentSegment(track.getId()); long idleTime = 0L; if (lastValidTrackPoint != null && location.getTime() > lastValidTrackPoint.getTime()) { idleTime = location.getTime() - lastValidTrackPoint.getTime(); } locationListenerPolicy.updateIdleTime(idleTime); if (currentRecordingInterval != locationListenerPolicy.getDesiredPollingInterval()) { registerLocationListener(); } Sensor.SensorDataSet sensorDataSet = getSensorDataSet(); if (sensorDataSet != null) { location = new MyTracksLocation(location, sensorDataSet); } // Always insert the first segment location if (!currentSegmentHasLocation) { insertLocation(track, location, null); currentSegmentHasLocation = true; lastLocation = location; return; } if (!LocationUtils.isValidLocation(lastValidTrackPoint)) { /* * Should not happen. The current segment should have a location. Just * insert the current location. */ insertLocation(track, location, null); lastLocation = location; return; } double distanceToLastTrackLocation = location.distanceTo(lastValidTrackPoint); if (distanceToLastTrackLocation > maxRecordingDistance) { insertLocation(track, lastLocation, lastValidTrackPoint); Location pause = new Location(LocationManager.GPS_PROVIDER); pause.setLongitude(0); pause.setLatitude(PAUSE_LATITUDE); pause.setTime(lastLocation.getTime()); insertLocation(track, pause, null); insertLocation(track, location, null); isIdle = false; } else if (sensorDataSet != null || distanceToLastTrackLocation >= recordingDistanceInterval) { insertLocation(track, lastLocation, lastValidTrackPoint); insertLocation(track, location, null); isIdle = false; } else if (!isIdle && location.hasSpeed() && location.getSpeed() < MAX_NO_MOVEMENT_SPEED) { insertLocation(track, lastLocation, lastValidTrackPoint); insertLocation(track, location, null); isIdle = true; } else if (isIdle && location.hasSpeed() && location.getSpeed() >= MAX_NO_MOVEMENT_SPEED) { insertLocation(track, lastLocation, lastValidTrackPoint); insertLocation(track, location, null); isIdle = false; } else { Log.d(TAG, "Not recording location, idle"); } lastLocation = location; } catch (Error e) { Log.e(TAG, "Error in onLocationChangedAsync", e); throw e; } catch (RuntimeException e) { Log.e(TAG, "RuntimeException in onLocationChangedAsync", e); throw e; } }
From source file:com.marianhello.cordova.bgloc.LocationUpdateService.java
public void onLocationChanged(Location location) { Log.d(TAG, "- onLocationChanged: " + location.getLatitude() + "," + location.getLongitude() + ", accuracy: " + location.getAccuracy() + ", isMoving: " + isMoving + ", speed: " + location.getSpeed()); if (!isMoving && !isAcquiringStationaryLocation && stationaryLocation == null) { // Perhaps our GPS signal was interupted, re-acquire a stationaryLocation now. setPace(false);// w w w . ja v a2s. c om } if (isDebugging) { Toast.makeText(this, "mv:" + isMoving + ",acy:" + location.getAccuracy() + ",v:" + location.getSpeed() + ",df:" + scaledDistanceFilter, Toast.LENGTH_LONG).show(); } if (isAcquiringStationaryLocation) { if (stationaryLocation == null || stationaryLocation.getAccuracy() > location.getAccuracy()) { stationaryLocation = location; } if (++locationAcquisitionAttempts == MAX_STATIONARY_ACQUISITION_ATTEMPTS) { isAcquiringStationaryLocation = false; startMonitoringStationaryRegion(stationaryLocation); if (isDebugging) { startTone("long_beep"); } } else { // Unacceptable stationary-location: bail-out and wait for another. if (isDebugging) { startTone("beep"); } return; } } else if (isAcquiringSpeed) { if (++locationAcquisitionAttempts == MAX_SPEED_ACQUISITION_ATTEMPTS) { // Got enough samples, assume we're confident in reported speed now. Play "woohoo" sound. if (isDebugging) { startTone("doodly_doo"); } isAcquiringSpeed = false; scaledDistanceFilter = calculateDistanceFilter(location.getSpeed()); setPace(true); } else { if (isDebugging) { startTone("beep"); } return; } } else if (isMoving) { if (isDebugging) { startTone("beep"); } // Only reset stationaryAlarm when accurate speed is detected, prevents spurious locations from resetting when stopped. if ((location.getSpeed() >= 1) && (location.getAccuracy() <= stationaryRadius)) { resetStationaryAlarm(); } // Calculate latest distanceFilter, if it changed by 5 m/s, we'll reconfigure our pace. Integer newDistanceFilter = calculateDistanceFilter(location.getSpeed()); if (newDistanceFilter != scaledDistanceFilter.intValue()) { Log.i(TAG, "- updated distanceFilter, new: " + newDistanceFilter + ", old: " + scaledDistanceFilter); scaledDistanceFilter = newDistanceFilter; setPace(true); } if (location.distanceTo(lastLocation) < distanceFilter) { return; } } else if (stationaryLocation != null) { return; } // Go ahead and cache, push to server lastLocation = location; // persistLocation(location); broadcastLocation(location); // if (this.isNetworkConnected()) { // Log.d(TAG, "Scheduling location network post"); // schedulePostLocations(); // } else { // Log.d(TAG, "Network unavailable, waiting for now"); // } }
From source file:com.spoiledmilk.ibikecph.search.SearchAutocompleteActivity.java
private void spawnSearchThreads(final Location loc, final String searchText, final Address addr, final String tag) { if (lastAddress != null && lastAddress.equals(addr) && adapter != null && adapter.getCount() != 0) { return;/*from ww w .j av a 2s .c o m*/ } else { lastAddress = addr; } if (!Util.isNetworkConnected(this)) { Util.launchNoConnectionDialog(this); progressBar.setVisibility(View.INVISIBLE); } else { // fetch the Kortforsyningen autocomplete kmsThread = new Thread(new Runnable() { @Override public void run() { // final List<JsonNode> kortforsyningenList = new ArrayList<JsonNode>(); final ArrayList<SearchListItem> data = new ArrayList<SearchListItem>(); if (!(addr.street == null || addr.street.trim().equals(""))) { List<JsonNode> list = HTTPAutocompleteHandler.getKortforsyningenAutocomplete(loc, addr); int count = 0; if (list != null) { for (JsonNode node : list) { if (count == 10) { break; } KortforData kd = new KortforData(node); if (kd.getCity() != null && addr.city != null && kd.getCity().toLowerCase(Locale.US).contains(addr.city)) { LOG.d("kd = " + kd); } if (addr.zip != null && !addr.zip.equals("") && kd.getZip() != null) { if (!addr.zip.trim().toLowerCase(Locale.UK) .equals(kd.getZip().toLowerCase(Locale.UK))) { continue; } } LOG.d("kd = " + kd); if (kd.getCity() != null && addr.city != null && kd.getCity().toLowerCase(Locale.US).contains(addr.city) && kd.getCity().contains("Aarhus")) { LOG.d("kd.city = " + kd.getCity() + " addr city = " + addr.city); } if (addr.city != null && !addr.city.equals("") && !addr.city.equals(addr.street) && kd.getCity() != null) { if (!(addr.city.trim().toLowerCase(Locale.UK) .contains(kd.getCity().toLowerCase(Locale.UK)) || kd.getCity().trim().toLowerCase(Locale.UK) .contains(addr.city.toLowerCase(Locale.UK)))) { continue; } } LOG.d("adding a kd to the list " + kd); data.add(kd); count++; } } } if (!addr.isAddress()) { List<JsonNode> places = HTTPAutocompleteHandler.getKortforsyningenPlaces(loc, addr); if (places != null) { int count = 0; if (places != null) { LOG.d("places count = " + places.size() + " data = " + places.toString()); for (JsonNode node : places) { if (count == 10) { break; } KortforData kd = new KortforData(node); if (addr.zip != null && !addr.zip.equals("") && kd.getZip() != null) { if (!addr.zip.trim().toLowerCase(Locale.UK) .equals(kd.getZip().toLowerCase(Locale.UK))) { continue; } } if (addr.city != null && !addr.city.equals("") && !addr.city.equals(addr.street) && kd.getCity() != null) { if (!(addr.city.trim().toLowerCase(Locale.UK) .contains(kd.getCity().toLowerCase(Locale.UK)) || kd.getCity().trim().toLowerCase(Locale.UK) .contains(addr.city.toLowerCase(Locale.UK)))) { continue; } } data.add(kd); count++; } } } } isOirestFetched = true; runOnUiThread(new Runnable() { public void run() { updateListData(data, tag, addr); } }); } }); kmsThread.start(); if (textSrch.getText().toString().length() >= 3 && addr.isFoursquare()) { // fetch the Foursquare autocomplete foursquareThread = new Thread(new Runnable() { @Override public void run() { List<JsonNode> list = HTTPAutocompleteHandler.getFoursquareAutocomplete(addr, SearchAutocompleteActivity.this, loc); final ArrayList<SearchListItem> data = new ArrayList<SearchListItem>(); if (list != null) { int count = 0; for (JsonNode node : list) { if (count == 3) { break; } JsonNode location = node.path("location"); if (location.has("lat") && location.has("lng") && location.get("lat").asDouble() != 0 && location.get("lng").asDouble() != 0) { String country = location.has("country") ? location.get("country").asText() : ""; if (country.contains("Denmark") || country.contains("Dansk") || country.contains("Danmark")) { FoursquareData fd = new FoursquareData(node); fd.setDistance(loc.distanceTo( Util.locationFromCoordinates(fd.getLatitude(), fd.getLongitude()))); data.add(fd); count++; } } } } isFoursquareFetched = true; runOnUiThread(new Runnable() { public void run() { updateListData(data, tag, addr); } }); } }); foursquareThread.start(); } else { isFoursquareFetched = true; } } }