简体   繁体   English

允许模拟位置#Android

[英]Allow mock Locations #Android

I have been trying to use this feature available under the developer options on a android device. 我一直在尝试使用Android设备上开发人员选项下可用的此功能。 i am a tester so trying to fake a location on android device. 我是一名测试人员,因此尝试在Android设备上伪造一个位置。 I have already given permission ACCESS_MOCK_LOCATION under android manifest permissions. 我已经在android manifest权限下授予了ACCESS_MOCK_LOCATION权限。 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. 我是代码和测试人员的新手,所以不知道我应该为伪造的long和lat写什么代码,以及应该放在哪里。 I have the source code for my test app and its a health app with maps on the home screen. 我有我的测试应用程序的源代码及其在主屏幕上带有地图的运行状况应用程序。

After instantiating your LocationClient you can call locationclient.setMockMode(true); 实例化LocationClient ,可以调用locationclient.setMockMode(true);

After that you can have your code generate Location objects like so 之后,您可以让代码生成如下的Location对象

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. 博客介绍了在调试时如何使用模拟位置以及在不调试时如何将其关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM