Add and use new LocationHelper
This commit is contained in:
parent
6fc0e2d634
commit
2165785346
56
api/src/org/microg/nlp/api/LocationHelper.java
Normal file
56
api/src/org/microg/nlp/api/LocationHelper.java
Normal file
|
@ -0,0 +1,56 @@
|
|||
package org.microg.nlp.api;
|
||||
|
||||
import android.location.Location;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public final class LocationHelper {
|
||||
private LocationHelper() {
|
||||
}
|
||||
|
||||
public static Location create(String source) {
|
||||
return new Location(source);
|
||||
}
|
||||
|
||||
public static Location create(String source, double latitude, double longitude, float accuracy) {
|
||||
Location location = create(source);
|
||||
location.setLatitude(latitude);
|
||||
location.setLongitude(longitude);
|
||||
location.setAccuracy(accuracy);
|
||||
return location;
|
||||
}
|
||||
|
||||
public static Location create(String source, double latitude, double longitude, double altitude, float accuracy) {
|
||||
Location location = create(source, latitude, longitude, accuracy);
|
||||
location.setAltitude(altitude);
|
||||
return location;
|
||||
}
|
||||
|
||||
public static Location average(String source, Collection<Location> locations) {
|
||||
if (locations == null || locations.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
int num = locations.size();
|
||||
double latitude = 0;
|
||||
double longitude = 0;
|
||||
float accuracy = 0;
|
||||
int altitudes = 0;
|
||||
double altitude = 0;
|
||||
for (Location value : locations) {
|
||||
if (value != null) {
|
||||
latitude += value.getLatitude();
|
||||
longitude += value.getLongitude();
|
||||
accuracy += value.getAccuracy();
|
||||
if (value.hasAltitude()) {
|
||||
altitude += value.getAltitude();
|
||||
altitudes++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (altitudes > 0) {
|
||||
return create(source, latitude / num, longitude / num, altitude / altitudes, accuracy / num);
|
||||
} else {
|
||||
return create(source, latitude / num, longitude / num, accuracy / num);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,20 +3,18 @@ package org.microg.nlp.api.sample;
|
|||
import android.location.Location;
|
||||
import android.util.Log;
|
||||
import org.microg.nlp.api.LocationBackendService;
|
||||
import org.microg.nlp.api.LocationHelper;
|
||||
|
||||
public class SampleBackendService extends LocationBackendService {
|
||||
private static final String TAG = SampleBackendService.class.getName();
|
||||
|
||||
@Override
|
||||
protected Location update() {
|
||||
if (System.currentTimeMillis()%60000>2000) {
|
||||
if (System.currentTimeMillis() % 60000 > 2000) {
|
||||
Log.d(TAG, "I decided not to answer now...");
|
||||
return null;
|
||||
}
|
||||
Location location = new Location("sample");
|
||||
location.setLatitude(42);
|
||||
location.setLongitude(42);
|
||||
location.setAccuracy(42);
|
||||
Location location = LocationHelper.create("sample", 42, 42, 42);
|
||||
Log.d(TAG, "I was asked for location and I answer: " + location);
|
||||
return location;
|
||||
}
|
||||
|
|
|
@ -3,16 +3,14 @@ package org.microg.nlp.api.sample;
|
|||
import android.location.Location;
|
||||
import android.util.Log;
|
||||
import org.microg.nlp.api.LocationBackendService;
|
||||
import org.microg.nlp.api.LocationHelper;
|
||||
|
||||
public class SecondSampleService extends LocationBackendService {
|
||||
private static final String TAG = SecondSampleService.class.getName();
|
||||
|
||||
@Override
|
||||
protected Location update() {
|
||||
Location location = new Location("second-sample");
|
||||
location.setLatitude(13);
|
||||
location.setLongitude(13);
|
||||
location.setAccuracy((System.currentTimeMillis() / 1000) % 100);
|
||||
Location location = LocationHelper.create("second-sample", 13, 13, (System.currentTimeMillis() / 1000) % 100);
|
||||
Log.d(TAG, "I was asked for location and I answer: " + location);
|
||||
return location;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.microg.nlp.api.sample;
|
|||
import android.location.Location;
|
||||
import android.util.Log;
|
||||
import org.microg.nlp.api.LocationBackendService;
|
||||
import org.microg.nlp.api.LocationHelper;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
|
@ -25,10 +26,7 @@ public class ThirdSampleService extends LocationBackendService {
|
|||
} catch (InterruptedException e) {
|
||||
return;
|
||||
}
|
||||
Location location = new Location("random");
|
||||
location.setLatitude(random.nextDouble() * 90);
|
||||
location.setLongitude(random.nextDouble() * 90);
|
||||
location.setAccuracy(random.nextFloat() * 90);
|
||||
Location location = LocationHelper.create("random", random.nextDouble() * 90, random.nextDouble() * 90, random.nextFloat() * 90);
|
||||
Log.d(TAG, "Just reported: " + location);
|
||||
report(location);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue