LearningCode
LearningCode

Reputation: 59

Allow mock Locations #Android

I have been trying to use this feature available under the developer options on a android device. i am a tester so trying to fake a location on android device. I have already given permission ACCESS_MOCK_LOCATION under android manifest permissions. Not sure what and where should i put my fake longitude and latitude in my code. does anyone have tried it before? I am new to code and a tester so don't have idea what code should i write for fake long and lat and where should i put it. I have the source code for my test app and its a health app with maps on the home screen.

Upvotes: 0

Views: 911

Answers (1)

Dave
Dave

Reputation: 1874

After instantiating your LocationClient you can call locationclient.setMockMode(true);

After that you can have your code generate Location objects like so

public Location createLocation(double lat, double lng, float accuracy) {
    // Create a new Location
    Location newLocation = new Location(PROVIDER);
    newLocation.setLatitude(lat);
    newLocation.setLongitude(lng);
    newLocation.setAccuracy(accuracy);
    return newLocation;
}

After that you could do something like

Location testLocation = createLocation(12.34, 45.679, 9.0f);

and

locationClient.setMockLocation(testLocation);

(This is taken from here)

This blog describes how to use mock locations when debugging and turning them off when not debugging.

Upvotes: 1

Related Questions