如何使用模拟位置的Google导航

时间:2013-06-24 16:47:15

标签: android google-maps locationmanager

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

谢谢!

1 个答案:

答案 0 :(得分:0)

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

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

> geo fix latitude* longitude* height*
例如

*(没有高度):

  

geo fix 7.1534 54.46343