views:

2271

answers:

4

I want to get longitude and latitude in android emulator for testing.

Can any one guide me how to achieve this?

How do I set the location of the emulator to a test position?

+2  A: 

Assuming you've got a mapview set up and running:

MapView mapView = (MapView) findViewById(R.id.mapview);
final MyLocationOverlay myLocation = new MyLocationOverlay(this, mapView);

mapView.getOverlays().add(myLocation);
myLocation.enableMyLocation();

myLocation.runOnFirstFix(new Runnable() {
    public void run() {
        GeoPoint pt = myLocation.getMyLocation();
    }
});

You'll need the following permission in your manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

And to send mock coordinates to the emulator from Eclipse, Go to the "Window" menu, select "Show View" > "Other" > "Emulator control", and you can send coordinates from the emulator control pane that appears.

David Hedlund
but my friend where are the longitude and latitude ??
UMMA
i have tried using this ans sending mock values to emulator control but when i press send button it does not do any thing no message nothing appears to confirm values sent or not.seocnd thing GeoPoint g= myLocation.getMyLocation();is returning null value.
UMMA
Are you sure that you're using it within `runOnFirstFix`? Because that's the event that's raised when the device first receives GPS position, so if that's ever raised, `getMyLocation` should definitely return a value.
David Hedlund
using above code oncreateand noticed that runOnFirstFix is not being called...
UMMA
+3  A: 

You can connect to the Emulator via Telnet. You then have a Emulator console that lets you enter certain data like geo fixes, network etc.

How to use the console is extensively explained here. To connect to the console open a command line and type

telnet localhost 5554

You then can use the geo command to set a latitude, longitude and if needed altitude on the device that is passed to all programs using the gps location provider. See the link above for further instructions.

If you need more then one coordinate you can use a kml file with a route as well it is a little bit described in this article. I can't find a better source at the moment.

Janusz
using geo fix i have also set mock latitude and longitude still getting null location.
UMMA
adding many values through telnet now it is working.. i dont know why entering two or three values it was not working..it works untill i close the emulator when i close the emulator i have to enter mock values again.
UMMA
+1  A: 

I was trying to set the geo fix through adb for many points and could not get my app to see any GPS data. But when I tried opening DDMS, selecting my app's process and sending coordinates through the emulator control tab it worked right away.

James
A: 

I was looking for a better way to set the emulator's GPS coordinates than using geo fix and manually determining the specific latitude and longitude coordinates.

Unable to find anything, I put together a little program that uses GWT and the Google Maps API to launch a browser-based map tool to set the GPS location in the emulator:

android-gps-emulator

Hopefully it can be of use to help others who will undoubtedly stumble across this difficulty/question as well.

android-gps-emulator


Notes:

  • mercurial repository
  • requires Maven
  • doesn't currently support multiple coordinates (but feel free to contribute!)
dpdearing