Reputation: 221
I'm verry new to android app creation, so i have a question:
Could you please tell me why real android device won't recognize mock (fake) location that is taken from jSON?
Function:
public void setLocation(double latitude, double longitude,Context ctx) {
LocationManager mLocationManager = (LocationManager) ctx.getSystemService(Service.LOCATION_SERVICE);
if (mLocationManager.getProvider("spoof") == null) {
mLocationManager.addTestProvider("spoof", false, true, false, false, false, false, false, 0, 5);
mLocationManager.setTestProviderEnabled("spoof", true);
}
Location loc = new Location("spoof");
loc.setLatitude(latitude);
loc.setLongitude(longitude);
loc.setTime(System.currentTimeMillis());
loc.setAccuracy(16F);
loc.setAltitude(0D);
loc.setBearing(0F);
mLocationManager.setTestProviderLocation("spoof", loc);
Toast.makeText(getBaseContext(), "Lat:"+latitude+" - Lng:"+longitude, Toast.LENGTH_LONG).show();
}
So now i have double
latitude and longitude and they are set as lata
, longa
Here is the code that runs the function
setLocation(lata,longa, getBaseContext());
Function and sending data to function works, 'cause it prints out lat and long with Toast.makeText
, but the problem is that google maps, google navigator won't see the new location and set is the real one.. it uses network location instead..
I've tried to run this on devices without GPS - Android 4.0.3 - Android 4.2.2
Thank you in advance!
Upvotes: 0
Views: 2551
Reputation: 3083
I think I got the point, well what I did for this specific problem of yours is the following:
You have to create a class which extends the LocationSource like in the following example:
public class MyLocationSource implements LocationSource{
//either save it or add it to a list
//adivsable would be to create one instance for each listener not an array.
OnLocationChangedListener listener;
public MyLocationSource () {
}
//Google maps somehow extends this OnLocationChangedListener
@Override
public void activate(OnLocationChangedListener mListener) {
this.listener = mListener;
//initially u save the listener
//here u can make a mix between what the GPS delivers and your manual loc
}
@Override
public void deactivate() {
//here u can stop the updates
listener = null;
}
protected void fireUpdate(Location loc) {
if (listener != null && loc != null) {
listener.onLocationChanged(loc);
}
}
}
Now all you have to do in google maps is to instantiate this class and try providing with the locations you want using what I gave you.
More details about activate/de-activate you can find at the following address:
http://developer.android.com/reference/com/google/android/gms/maps/LocationSource.html
Additionally you can take a look at commonswguy:
https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Location
Upvotes: 1