Reputation: 323
I created an Android application that mock the gps location, its works great with most of the programs like igo, waze, osmAnd, GPS status and more, but not with google maps that still show the actual position I'm on.
I disabled the gps and network location as well.
Any ideas why is it? how to solve it? Thanks
The code I use:
public void setLocation(double latitude, double longitude, double altitude, float bearing, float speed){
String PROVIDER_NAME = LocationManager.GPS_PROVIDER;
locationManager.addTestProvider(PROVIDER_NAME, false, false, false, false, true, true, true, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
locationManager.setTestProviderEnabled(PROVIDER_NAME, true);
locationManager.setTestProviderStatus(PROVIDER_NAME, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
Location location = new Location(PROVIDER_NAME);
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setAltitude(altitude);
location.setBearing(bearing);
location.setSpeed(speed);
location.setTime(System.currentTimeMillis());
locationManager.setTestProviderLocation(PROVIDER_NAME, location.getLocation());
}
Upvotes: 3
Views: 9355
Reputation: 323
Thanks for the help.
I found the problem, seems that goggle requires to set the Accuracy when other's apps don't need it
so adding the line location.setAccuracy(1);
It solved the issue.
Upvotes: 2
Reputation: 3824
You should rather use GoogleMap.setLocationSource(LocationSource source)
for providing random locations to the latest Google Maps SDK on Android. Use a random location source like this:
// In your MapFragment implementation or similar
if (BuildConfig.DEBUG)
map.setLocationSource(new MockLocationSource());
MockLocationSource.java:
public class MockLocationSource implements LocationSource {
private static final float ACCURACY = 1; // Meters
private static final int MAX_SPEED = 10; // m/s
private static final LatLng CENTER = new LatLng(59.91127, 10.70834);
private static final double DELTA_LAT = 0.0005;
private static final double DELTA_LON = 0.0005;
private static final long UPDATE_PERIOD = TimeUnit.SECONDS.toMillis(2);
private final Handler handler = new Handler();
private LatLng lastCoordinate = CENTER;
private OnLocationChangedListener listener;
private void scheduleNewFix() {
handler.postDelayed(updateLocationRunnable, UPDATE_PERIOD);
}
private final Runnable updateLocationRunnable = new Runnable() {
@Override
public void run() {
Location randomLocation = generateRandomLocation();
listener.onLocationChanged(randomLocation);
scheduleNewFix();
}
};
public Location generateRandomLocation() {
Location location = new Location(getClass().getSimpleName());
location.setTime(System.currentTimeMillis());
location.setAccuracy(ACCURACY);
location.setBearing(randomizer.nextInt(360));
location.setSpeed(randomizer.nextInt(MAX_SPEED));
location.setLatitude(lastCoordinate.latitude + scaleOffset(DELTA_LAT));
location.setLongitude(lastCoordinate.longitude + scaleOffset(DELTA_LON));
lastCoordinate = new LatLng(location.getLatitude(), location.getLongitude());
return location;
}
private final static Random randomizer = new Random();
private double scaleOffset(double value) {
return (randomizer.nextDouble() - 0.5) * value;
}
@Override
public void activate(OnLocationChangedListener locationChangedListener) {
listener = locationChangedListener;
scheduleNewFix();
}
@Override
public void deactivate() {
handler.removeCallbacks(updateLocationRunnable);
}
}
Upvotes: 3