How to emulate GPS location in the Android Emulato

2018-12-31 02:23发布

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?

30条回答
呛了眼睛熬了心
2楼-- · 2018-12-31 03:04

You can use an emulator like genymotion which gives you the flexibility to emulate your present GPS location, etc.

查看更多
低头抚发
3楼-- · 2018-12-31 03:04

The already mentioned multiple times answer to use the shell command "geo fix..." is the correct answer. But in case you use LocationClient.getLastLocation() to retrieve your data it is worth to mention that it will not work at first. The LocationClient class uses the Google Play Service to retrieve the coordinates. For me this started working after running the emulators maps app once. During the first start you are asked to allow google apps access to your location, which I guess does the trick.

查看更多
美炸的是我
4楼-- · 2018-12-31 03:05

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.

查看更多
人间绝色
5楼-- · 2018-12-31 03:05

In Linux where communication ports are blocked. navigate the terminal to platform-tools folder inside android sdk and fire this command:

./adb -s #{device_name} emu geo fix #{longitude} #{latitude}
查看更多
有味是清欢
6楼-- · 2018-12-31 03:05

If you're using Android Studio (1.3):

  • Click on Menu "Tools"
  • "Android"
  • "Android device monitor"
  • click on your current Emulator
  • Tab "Emulator Control"
  • go to "Location Controls" and enter Lat and Lon
查看更多
妖精总统
7楼-- · 2018-12-31 03:06

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.

The specific command to run in the console is

geo fix <longitude value> <latitude value>

I found this site useful for finding a realistic lat/lng: http://itouchmap.com/latlong.html

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.

查看更多
登录 后发表回答