简体   繁体   English

如何在 Android 模拟器中模拟 GPS 位置?

[英]How to emulate GPS location in the Android Emulator?

I want to get longitude and latitude in Android emulator for testing.我想在Android模拟器中获取经纬度进行测试。

Can any one guide me how to achieve this?谁能指导我如何实现这一目标?

How do I set the location of the emulator to a test position?如何将模拟器的位置设置为测试 position?

You can connect to the Emulator via Telnet.您可以通过 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.然后,您可以使用 geo 命令在设备上设置纬度、经度和海拔(如果需要),这些海拔高度会通过 gps 位置提供程序传递给所有程序。 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我发现这个网站对于找到一个真实的 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 .如果您需要多个坐标,您也可以使用带有路线的 kml 文件, 本文对此进行了一些描述。 I can't find a better source at the moment.我目前找不到更好的来源。

No one here mentioned the built in solution of the emulator itself, so for future visitors, I'd like to share it with visuals.这里没有人提到模拟器本身的内置解决方案,因此对于未来的访问者,我想通过视觉效果分享它。

First, run your Android Emulator and click on the menu button (3 dots) shown below:首先,运行您的 Android 模拟器并单击如下所示的菜单按钮(3 个点):

带菜单按钮的模拟器

Then from the left pane, select Location and change the coordinates according to your needs.然后从左窗格中选择位置并根据需要更改坐标。 After pressing Send button, changes will immediately take effect (I recommend you to open up Google Maps for better understanding).按下发送按钮后,更改将立即生效(建议您打开谷歌地图以便更好地理解)。

在此处输入图片说明

Android Studio Version: 2.3.3安卓工作室版本:2.3.3

In addition, to make your different locations coming to your application in real time, you can use GPX file.此外,为了让您的不同位置实时进入您的应用程序,您可以使用 GPX 文件。 It's very easy to create this file from Google Map direction link:从谷歌地图方向链接创建这个文件非常容易:

  1. Go to google map, choose a location, then press "Directions" and enter the second location.转到谷歌地图,选择一个位置,然后按“路线”并输入第二个位置。
  2. After route is created, copy a link from the browser创建路由后,从浏览器复制一个链接
  3. Go to this website: https://mapstogpx.com and paste the link to "Let's Go" box转到此网站: https : //mapstogpx.com并将链接粘贴到“Let's Go”框
  4. Press the "Let's Go" button and GPX file will be downloaded按“Let's Go”按钮,将下载 GPX 文件

Use "Load GPS/KML" button to load the created file to your emulator, choose speed, and press green play button on the bottom.使用“加载 GPS/KML”按钮将创建的文件加载到您的模拟器,选择速度,然后按底部的绿色播放按钮。 Locations will be sent in real time as shown on the picture below.位置将实时发送,如下图所示。

在此处输入图片说明

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.我正在寻找一种比使用geo fix和手动确定特定纬度和经度坐标更好的方法来设置模拟器的 GPS 坐标。

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:找不到任何东西,我整理了一个使用 GWT 和 Google Maps API 的小程序来启动基于浏览器的地图工具来在模拟器中设置 GPS 位置:

android-gps-emulator android-gps-模拟器

Hopefully it can be of use to help others who will undoubtedly stumble across this difficulty/question as well.希望它可以帮助那些毫无疑问也会遇到这个困难/问题的人。

如果您使用的是Eclipse ,请转到Window->Open Perspective->DDMS ,然后在Location Controls键入一个并点击Send

For Android Studio users:对于 Android Studio 用户:

run the emulator,运行模拟器,

Then, go to Tools -> Android ->Android device monitor然后,转到Tools -> Android ->Android device monitor

open the Emulator Control Tab, and use the location controls group.打开Emulator Control选项卡,并使用位置控制组。

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.并且要从 Eclipse 向模拟器发送模拟坐标,请转到“窗口”菜单,选择“显示视图”>“其他”>“模拟器控件”,您可以从出现的模拟器控制面板发送坐标。

For the new emulator:对于新模拟器:

http://developer.android.com/tools/devices/emulator.html#extended http://developer.android.com/tools/devices/emulator.html#extended

Basically, click on the three dots button in the emulator controls (to the right of the emulator) and it will open up a menu which will allow you to control the emulator including location基本上,单击模拟器控件中的三个点按钮(模拟器右侧),它将打开一个菜单,允许您控制模拟器,包括位置

1. Android Studio users. 1. Android Studio 用户。

After running the emulator goto Tools->Android->Android device monitor运行模拟器后转到 Tools->Android->Android device monitor

Click the Emulator Control Tab change from the location controls group.单击位置控制组中的模拟器控制选项卡更改。

2. Eclipse users. 2. Eclipse 用户。

First In Eclipse In Menu Select "Window" then Select "Open Perspective" then Select "DDMS".首先在Eclipse中的菜单中选择“Window”,然后选择“Open Perspective”,然后选择“DDMS”。 ie Window->Open Prespective->DDMS.即窗口->打开预定义->DDMS。

You will see on Left Side Devices Panel and on Right Side you will see different tabs.您将在左侧设备面板和右侧看到不同的选项卡。 Select "Emulator Control" Tab.选择“模拟器控制”选项卡。

At bottom you will see Location Controls Panel.在底部,您将看到位置控制面板。 Select "Manual" Tab.选择“手动”选项卡。

Enter Longitude and Latitude in Textboxs then Click Send Button.在文本框中输入经度和纬度,然后单击发送按钮。 It will send the position to you emulator and the application.它会将位置发送给您的模拟器和应用程序。

3. Using telnet. 3. 使用远程登录。

In the run command type this.在运行命令中输入这个。

telnet localhost 5554

If you are not using windows you can use any telnet client.如果您不使用 Windows,您可以使用任何 telnet 客户端。

After connecting with telnet use the following command to send your position to emulator.与 telnet 连接后,使用以下命令将您的位置发送到模拟器。

geo fix long lat    
geo fix -121.45356 46.51119 4392

4. Use the browser based Google maps tool 4. 使用基于浏览器的谷歌地图工具

There is a program that uses GWT and the Google Maps API to launch a browser-based map tool to set the GPS location in the emulator:有一个程序使用 GWT 和 Google Maps API 来启动基于浏览器的地图工具来在模拟器中设置 GPS 位置:

android-gps-emulator android-gps-模拟器

Finally with the latest release of Android Studio 4 and his new Emulator update 10/23/2019 it become easier.最后,随着最新版本的 Android Studio 4 和他的新模拟器更新 10/23/2019,它变得更容易了。 Start your emulator and go to emulator parameters ... > in "Routes" tab you can choose two points on the map from/to and start a new route with an adjustable playback speed that can go to more than 1000km/h!启动您的模拟器并转到模拟器参数... > 在“路线”选项卡中,您可以选择地图上的两个点从/到并开始一条具有可调节播放速度的新路线,播放速度可以超过 1000 公里/小时!

在此处输入图片说明

以下解决方案对我有用 - 打开命令行并写入:

adb emu geo fix [longtitude] [latitude]

Using the "geo" command in the emulator console在模拟器控制台中使用“geo”命令

To send mock location data from the command line:从命令行发送模拟位置数据:

  1. Launch your application in the Android emulator and open a terminal/console in your SDK's /tools directory.在 Android 模拟器中启动您的应用程序并在您的 SDK 的 /tools 目录中打开一个终端/控制台。

  2. Connect to the emulator console:连接到模拟器控制台:

    telnet localhost 5555 (Replace 5555 with whatever port your emulator is running on) telnet localhost 5555 (用模拟器运行的任何端口替换 5555)

  3. Send the location data: * geo fix to send a fixed geo-location.发送位置数据: * geo fix 发送固定的地理位置。

    This command accepts a longitude and latitude in decimal degrees, and an optional altitude in meters.此命令接受以十进制度数表示的经度和纬度,以及以米为单位的可选高度。 For example:例如:

     geo fix -121.45356 46.51119 4392

I wrote a python script to push gps locations to the emulator via telnet.我编写了一个 python 脚本,通过 telnet 将 gps 位置推送到模拟器。 It defines a source and a destination location.它定义了源位置和目标位置。 There is also a time offset which lets you control how long coordinates will be pushed to the device.还有一个时间偏移,可让您控制将坐标推送到设备的时间长度。 One location is beeing pushed once a second.一个位置每秒被推送一次。

In the example below the script moves from Berlin to Hamburg in 120 seconds.在下面的示例中,脚本在 120 秒内从柏林移动到汉堡。 One step/gps location per second with random distances.每秒一步/gps 位置随机距离。

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import telnetlib
from time import sleep
import random

HOST = "127.0.0.1"
PORT = 5554
TIMEOUT = 10
LAT_SRC = 52.5243700
LNG_SRC = 13.4105300
LAT_DST = 53.5753200
LNG_DST = 10.0153400
SECONDS = 120

LAT_MAX_STEP = ((max(LAT_DST, LAT_SRC) - min(LAT_DST, LAT_SRC)) / SECONDS) * 2
LNG_MAX_STEP = ((max(LNG_DST, LNG_SRC) - min(LNG_DST, LNG_SRC)) / SECONDS) * 2

DIRECTION_LAT = 1 if LAT_DST - LAT_SRC > 0 else -1
DIRECTION_LNG = 1 if LNG_DST - LNG_SRC > 0 else -1

lat = LAT_SRC
lng = LNG_SRC

tn = telnetlib.Telnet(HOST, PORT, TIMEOUT)
tn.set_debuglevel(9)
tn.read_until("OK", 5)

tn.write("geo fix {0} {1}\n".format(LNG_SRC, LAT_SRC))
#tn.write("exit\n")

for i in range(SECONDS):
    lat += round(random.uniform(0, LAT_MAX_STEP), 7) * DIRECTION_LAT
    lng += round(random.uniform(0, LNG_MAX_STEP), 7) * DIRECTION_LNG

    #tn.read_until("OK", 5)
    tn.write("geo fix {0} {1}\n".format(lng, lat))
    #tn.write("exit\n")
    sleep(1)

tn.write("geo fix {0} {1}\n".format(LNG_DST, LAT_DST))
tn.write("exit\n")

print tn.read_all()

In Mac, Linux or Cygwin:在 Mac、Linux 或 Cygwin 中:

echo 'geo fix -99.133333 19.43333 2202' | nc localhost 5554

That will put you in Mexico City.那会让你在墨西哥城。 Change your longitude/latitude/altitude accordingly.相应地更改您的经度/纬度/高度。 That should be enough if you are not interested in nmea.如果您对 nmea 不感兴趣,那应该就足够了。

I use eclipse plug DDMS function to send GPS.我使用eclipse插件DDMS功能发送GPS。在此处输入图片说明

In Linux where communication ports are blocked.在 Linux 中,通信端口被阻止。 navigate the terminal to platform-tools folder inside android sdk and fire this command:将终端导航到android sdk platform-tools 文件夹并执行以下命令:

./adb -s #{device_name} emu geo fix #{longitude} #{latitude}

首先进入 Eclipse 中的 DDMS 部分,然后打开模拟器控制 .... 转到手动部分设置经纬度,然后按发送按钮

See Obtaining User Location请参阅获取用户位置

Look under Providing Mock Location Data .提供模拟位置数据下查看。 You will find the solution for it.你会找到它的解决方案。

Dalvik Debug Monitor > Select Emulator > Emulator Control Tab > Location Controls. Dalvik 调试监视器 > 选择模拟器 > 模拟器控制选项卡 > 位置控制。

DDMS -- android_sdk/tools/ddms OR android_sdk/tools/monitor DDMS -- android_sdk/tools/ddms 或 android_sdk/tools/monitor

I was trying to set the geo fix through adb for many points and could not get my app to see any GPS data.我试图通过 adb 为许多点设置地理定位,但无法让我的应用程序查看任何 GPS 数据。 But when I tried opening DDMS, selecting my app's process and sending coordinates through the emulator control tab it worked right away.但是当我尝试打开 DDMS,选择我的应用程序的进程并通过模拟器控制选项卡发送坐标时,它立即起作用。

如果您使用的是 eclipse 然后使用模拟器控制器,您可以手动设置纬度和经度并在模拟器中运行基于地图的应用程序

If you're using Android Studio (1.3):如果您使用的是 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转到“位置控制”并输入纬度和经度

Just make Alberto Gaona's answer into one line只需将 Alberto Gaona 的答案写成一行

token=$(cat ~/.emulator_console_auth_token); cat <(echo -e "auth $token \n  geo fix 96.0290791 16.9041016  \n exit") - | nc localhost 5554

5554 is the emulator port number shown in adb devices . 5554 是adb devices显示的模拟器端口号。

It would have been better if adb emu work.如果adb emu工作会更好。

If the above solutions don't work.如果上述解决方案不起作用。 Try this:尝试这个:

Inside your android Manifest.xml, add the following two links OUTSIDE of the application tag, but inside your manifest tag of course在您的 android Manifest.xml 中,在应用程序标签的外部添加以下两个链接,但当然是在您的清单标签内

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

I was unable to get a GPS fix on the emulator when emulator was running Android image without Google APIs.当模拟器在没有 Google API 的情况下运行 Android 图像时,我无法在模拟器上获得 GPS 修复。 As soon as I changed the image to contain Google APIs all of the here mentioned ways to get a GPS fix worked.一旦我将图像更改为包含 Google API,这里提到的所有获得 GPS 修复的方法都有效。

Make sure you select an image with Google APIs when creating AVD.确保在创建 AVD 时选择带有 Google API 的图像。

The already mentioned multiple times answer to use the shell command " geo fix... " is the correct answer.已经多次提到使用 shell 命令“ geo fix... ”的答案是正确的答案。 But in case you use LocationClient.getLastLocation() to retrieve your data it is worth to mention that it will not work at first.但是,如果您使用LocationClient.getLastLocation()来检索您的数据,值得一提的是它一开始不会工作。 The LocationClient class uses the Google Play Service to retrieve the coordinates. LocationClient 类使用 Google Play 服务来检索坐标。 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.在第一次开始时,您会被要求允许谷歌应用程序访问您的位置,我想这可以解决问题。

For a project of my own, I developed an online service which can provide simulated location to the Android emulator.对于我自己的一个项目,我开发了一个在线服务,可以为 Android 模拟器提供模拟位置。

It uses geo nmea rather than geo fix which allows it to set speed, course, precise time etc. in addition to just lat/lon.它使用 geo nmea 而不是 geo fix,这允许它设置速度、路线、精确时间等,而不仅仅是纬度/经度。

The service requires the nc (netcat) command line utility and nothing else.该服务需要 nc (netcat) 命令行实用程序,仅此而已。

http://www.kisstech.ch/nmea/ http://www.kisstech.ch/nmea/

您可以使用像genymotion这样的模拟器,它可以让您灵活地模拟您当前的 GPS 位置等。

There is a plugin for Android Studio called “Mock Location Plugin”. Android Studio 有一个名为“Mock Location Plugin”的插件。 You can emulate multiple points with this plugin.您可以使用此插件模拟多个点。 You can find a detailed manual of use in this link: Android Studio.您可以在此链接中找到详细的使用手册: Android Studio。 Simulate multiple GPS points with Mock Location Plugin 使用 Mock Location Plugin 模拟多个 GPS 点

Can't comment yet, so updating @ectomorphs answer here, which when telneting now requires to have an auth token.还不能发表评论,所以在这里更新 @ectomorphs 答案,现在 telnet 需要有一个 auth 令牌。 In linux that's under /home/username/.emulator_console_auth_token在 /home/username/.emulator_console_auth_token 下的 linux 中

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import telnetlib
from time import sleep
import random

FILE = open('/home/username/.emulator_console_auth_token', 'r')
AUTH_TOKEN = FILE.read()
FILE.close()

HOST = "127.0.0.1"
PORT = 5554
TIMEOUT = 10
LAT_SRC = 52.5243700
LNG_SRC = 13.4105300
LAT_DST = 53.5753200
LNG_DST = 10.0153400
SECONDS = 120

LAT_MAX_STEP = ((max(LAT_DST, LAT_SRC) - min(LAT_DST, LAT_SRC)) / SECONDS) * 2
LNG_MAX_STEP = ((max(LNG_DST, LNG_SRC) - min(LNG_DST, LNG_SRC)) / SECONDS) * 2

DIRECTION_LAT = 1 if LAT_DST - LAT_SRC > 0 else -1
DIRECTION_LNG = 1 if LNG_DST - LNG_SRC > 0 else -1

lat = LAT_SRC
lng = LNG_SRC

tn = telnetlib.Telnet(HOST, PORT, TIMEOUT)
tn.set_debuglevel(9)
tn.read_until("OK", 5)

tn.write("auth {0}\n".format(AUTH_TOKEN))
tn.read_until("OK", 5)

tn.read_until("OK", 5)

tn.write("geo fix {0} {1}\n".format(LNG_SRC, LAT_SRC))
#tn.write("exit\n")

for i in range(SECONDS):
    lat += round(random.uniform(0, LAT_MAX_STEP), 7) * DIRECTION_LAT
    lng += round(random.uniform(0, LNG_MAX_STEP), 7) * DIRECTION_LNG

    #tn.read_until("OK", 5)
    tn.write("geo fix {0} {1}\n".format(lng, lat))
    #tn.write("exit\n")
    sleep(1)

tn.write("geo fix {0} {1}\n".format(LNG_DST, LAT_DST))
tn.write("exit\n")

print tn.read_all()

From a shell script one can set the coorinate like so从 shell 脚本可以像这样设置坐标

#!/usr/bin/env bash
export ATOKEN=`cat ~/.emulator_console_auth_token`
echo -ne "auth $ATOKEN\ngeo fix -99.133333 19.43333 2202\n"  | nc localhost 5554

打开 Android studio->Tools menu->Android->Android Device Monitor->Emulator Tab->Location control-> 设置你需要的经纬度并根据你的需要检查你的项目

Go to Extended controls in emulate.转到模拟中的Extended controls After You can then set the location for the emulate by searching or dragging the map to the location you want to set.之后您可以通过搜索或拖动地图到您要设置的位置来设置模拟的位置。

地点

Finally Click on SET LOCATION button to save.最后点击SET LOCATION按钮保存。

I want to get longitude and latitude in Android emulator for testing.我想在Android模拟器中获取经度和纬度以进行测试。

Can any one guide me how to achieve this?谁能指导我如何实现这一目标?

How do I set the location of the emulator to a test position?如何将仿真器的位置设置为测试位置?

I have made a little script similar to one of the previous answers, but using expect instead of python - because it is a bit simpler ( expect was invented for this).我制作了一个类似于以前的答案之一的小脚本,但是使用expect而不是python - 因为它更简单一些( expect就是为此发明的)。

#!/usr/bin/expect

set port [lindex $argv 0]
set lon [lindex $argv 1]
set lat [lindex $argv 2]

set timeout 1
spawn telnet localhost $port
expect_after eof { exit 0 }

## interact
expect OK

set fp [open "~/.emulator_console_auth_token" r]
if {[gets $fp line] != -1} {
  send "auth $line\r"
}

send "geo fix $lon $lat\r"
expect OK
send "exit\r"

Exemple usage: sendloc 5554 2.35607 48.8263 sendloc 5554 2.35607 48.8263用法: sendloc 5554 2.35607 48.8263

I had the same problem on Android studio 3.5.1 When I clicked on three dots to the right of android emulator and selected location tab, I dont see a send option.我在 Android studio 3.5.1 上遇到了同样的问题 当我点击 android 模拟器右侧的三个点和选择的位置选项卡时,我没有看到发送选项。 I tried to set a location point but that did not change my default cupertino location.我试图设置一个位置点,但这并没有改变我默认的库比蒂诺位置。 Open google maps and it will autolocate you and your cupertino location will change.打开谷歌地图,它会自动定位你,你的库比蒂诺位置会改变。

Sorry for the NecroPost, but after following some of the suggestions on this question, I set my location to Alaska.对 NecroPost 感到抱歉,但在遵循有关此问题的一些建议后,我将我的位置设置为阿拉斯加。 However, my device was still showing to be in Mountain View, California (Google's HQ?).但是,我的设备仍然显示在加利福尼亚州山景城(Google 的总部?)。 So here's how I did a fix:所以这是我修复的方法:

1) Go to the location settings: 1)进入位置设置:

在此处输入图片说明

2) Set your test location. 2) 设置您的测试位置。 I chose Alaska.我选择了阿拉斯加。

在此处输入图片说明

3) Google "My current location" and click on the map circled in the picture. 3) 谷歌“我的当前位置”并点击图片中圈出的地图。 Note that even though I set location as Alaska, my Virtual Device still thinks it's in Mountain View, California.请注意,即使我将位置设置为阿拉斯加,我的虚拟设备仍然认为它位于加利福尼亚州的山景城。

在此处输入图片说明

4) Click on this location Icon Your location should now be updated on your device. 4) 单击此位置图标您的位置现在应该会在您的设备上更新。 You can verify by Googling "My current location" again.您可以再次通过谷歌搜索“我的当前位置”进行验证。

在此处输入图片说明

If anyone experienced this same issue, I hope my solution helped you.如果有人遇到过同样的问题,希望我的解决方案对您有所帮助。

For BlueStack Users: open the emulator and press Ctrl + Shift + K then find your location and press set location !对于 BlueStack 用户:打开模拟器并按Ctrl + Shift + K然后找到您的位置并按set location

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM