0

我已经为我的 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,我将不胜感激。

谢谢!

4

1 回答 1

0

您必须手动设置位置。打开终端(在 Windows WIN+R 上,执行“cmd”)并输入:

> telnet localhost 5554  (depending on the Number displayed at you emulator)

> geo fix latitude* longitude* height*

*例如(没有高度):

地理修复 7.1534 54.46343

于 2013-06-26T15:41:33.967 回答