0

I have set up a MockLocationListener for my android app, but I'm kind of confused about h9ow too use it with Google Navigation. I'm not sure how Google Navigation will be able to pick up the fake location.

Here is my 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);
      }
    }

Here is my activity:

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();
  }
}

I'm not sure that I am using the mock locations properly with Google Navigation. I would appreciate it if someone could tell me how to use Google Navigation along with Mock Listener.

Thanks!

RagHaven
  • 4,156
  • 21
  • 72
  • 113
  • 1
    I think this one would help you [using-a-mock-location-in-navigation-app][1] [1]: http://stackoverflow.com/questions/7071625/using-a-mock-location-in-navigation-app – Scott Zhu Jun 24 '13 at 17:21

1 Answers1

0

You'll have to set the location manually. Open a terminal (on Windows WIN+R, execute "cmd") and type in:

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

> geo fix latitude* longitude* height*

*for example (without height):

geo fix 7.1534 54.46343

Cerdo
  • 33
  • 1
  • 1
  • 5