我已经为我的 android 应用程序设置了一个 MockLocationListener,但我对如何将它与 Google Navigation 一起使用感到有点困惑。我不确定 Google Navigation 将如何获取虚假位置。
这是我的 MockLocationProvider:
public class MockLocationProvider {
String providerName;
Context ctx;
public MockLocationProvider(String name, Context ctx) {
this.providerName = name;
this.ctx = ctx;
LocationManager lm = (LocationManager) ctx.getSystemService(
Context.LOCATION_SERVICE);
lm.addTestProvider(providerName, false, false, false, false, false,
true, true, 0, 5);
lm.setTestProviderEnabled(providerName, true);
}
public void pushLocation(double lat, double lon) {
LocationManager lm = (LocationManager) ctx.getSystemService(
Context.LOCATION_SERVICE);
Location mockLocation = new Location(providerName);
mockLocation.setLatitude(lat);
mockLocation.setLongitude(lon);
mockLocation.setAltitude(0);
mockLocation.setTime(System.currentTimeMillis());
lm.setTestProviderLocation(providerName, mockLocation);
}
public void shutdown() {
LocationManager lm = (LocationManager) ctx.getSystemService(
Context.LOCATION_SERVICE);
lm.removeTestProvider(providerName);
}
}
这是我的活动:
public class MainActivity extends Activity {
MockLocationProvider mock;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mock = new MockLocationProvider(LocationManager.NETWORK_PROVIDER, this);
//Set test location
mock.pushLocation(-12.34, 23.45);
LocationManager locMgr = (LocationManager)
getSystemService(LOCATION_SERVICE);
LocationListener lis = new LocationListener() {
public void onLocationChanged(Location location) {
//You will get the mock location
}
//...
};
locMgr.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000, 1, lis);
String dLat = "38.062419";
String dLong = "-99.173584";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" +dLat+","+dLong));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
protected void onDestroy() {
mock.shutdown();
super.onDestroy();
}
}
我不确定我是否在 Google 导航中正确使用了模拟位置。如果有人能告诉我如何使用 Google Navigation 和 Mock Listener,我将不胜感激。
谢谢!