如何在Android模拟器中模拟GPS位置?
我想在Android模拟器中获取经度和纬度进行测试。
任何人都可以指导我如何实现这一目标吗?
如何将模拟器的位置设置为测试位置?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
您可以通过 Telnet 连接到模拟器。然后,您将拥有一个模拟器控制台,可让您输入某些数据,例如地理修复、网络等。
如何使用控制台有详细解释 此处。
要连接到控制台,请打开命令行并键入
You then can use the geo command to set a latitude, longitude and if need elevation on the device that is pass to all applications using the gps locationprovider.请参阅上面的链接以获取更多说明。
在控制台中运行的具体命令是
我发现该站点对于查找实际的纬度/经度很有用: http://itouchmap .com/latlong.html
如果您需要多个坐标,您也可以使用带有路线的 kml 文件,在此 文章。我目前找不到更好的来源。
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
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
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.
这里没有人提到模拟器本身的内置解决方案,所以对于未来的访问者,我想用视觉来分享它。
首先,运行 Android 模拟器并单击如下所示的菜单按钮(3 个点):
然后从左侧窗格中选择位置并根据您的需要更改坐标。按发送按钮后,更改将立即生效(我建议您打开Google地图以便更好地理解)。
Android Studio 版本:2.3.3
另外,为了使您的不同位置实时到达您的应用程序,您可以使用 GPX 文件。从 Google 地图方向链接创建此文件非常容易:
使用“加载 GPS/KML”按钮将创建的文件加载到模拟器中,选择速度,然后按底部的绿色播放按钮。位置将实时发送,如下图所示。
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:
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
In addition, to make your different locations coming to your application in real time, you can use GPX file. It's very easy to create this file from Google Map direction link:
Use "Load GPS/KML" button to load the created file to your emulator, choose speed, and press green play button on the bottom. Locations will be sent in real time as shown on the picture below.
我一直在寻找一种比使用
geo fix
和手动确定特定纬度和经度坐标更好的方法来设置模拟器的 GPS 坐标。由于找不到任何东西,我编写了一个小程序,使用 GWT 和 Google Maps API 来启动基于浏览器的地图工具来在模拟器中设置 GPS 位置:
android-gps-emulator
希望它可以帮助其他无疑会遇到这个困难/问题的人出色地。
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.
如果您使用的是
Eclipse
,请转至Window->Open Perspective->DDMS
,然后在Location Controls
中输入 1 并点击 <代码>发送。If you're using
Eclipse
, go toWindow->Open Perspective->DDMS
, then type one inLocation Controls
and hitSend
.对于 Android Studio 用户:
运行模拟器,
然后转到
Tools -> Android ->Android 设备监视器
打开
模拟器控制
选项卡,然后使用位置控件组。For Android Studio users:
run the emulator,
Then, go to
Tools -> Android ->Android device monitor
open the
Emulator Control
Tab, and use the location controls group.对不起《NecroPost》。有些答案对我的具体问题没有帮助。
例如,我将位置设置为阿拉斯加。但是,我的设备仍然显示位于加利福尼亚州山景城(Google 总部)?
下面是我的修复方法:
1) 转到位置设置:
2) 设置您的测试位置。我选择了阿拉斯加。
3) Google“我的当前位置”,然后单击图中圈出的地图。
请注意,即使我将位置设置为阿拉斯加,我的虚拟设备仍然认为它位于加利福尼亚州山景城。
4) 单击此位置图标
您的位置现在应该在您的设备上更新。
您可以再次通过谷歌搜索“我的当前位置”进行验证。
如果有人遇到过同样的问题,我希望我的解决方案对您有所帮助。
Sorry for the NecroPost. Some of the answers did not help my specific issue.
For example, I set my location to Alaska. However, my device was still showing to be in Mountain View, California (Google's HQ)?
So here's how I did a fix:
1) Go to the location settings:
2) Set your test location. I chose Alaska.
3) Google "My current location" and click on the map circled in the picture.
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.
You can verify by Googling "My current location" again.
If anyone experienced this same issue, I hope my solution helped you.
以下解决方案对我有用 - 打开命令行并写入:
The following solution worked for me - open command line and write:
最后,随着 Android Studio 4 的最新版本及其新的模拟器更新 2019 年 10 月 23 日,一切变得更加容易。
启动模拟器并转到模拟器参数...>在“路线”选项卡中,您可以选择地图上的两个起点/终点,并开始一条新路线,播放速度可调节,播放速度可达 1000 公里/小时以上!
Finally with the latest release of Android Studio 4 and his new Emulator update 10/23/2019 it become easier.
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!
假设您已经设置并运行了地图视图:
您的清单中需要以下权限:
要将模拟坐标从 Eclipse 发送到模拟器,请转到“窗口”菜单,选择“显示视图”> 「其他」> “模拟器控制”,您可以从出现的模拟器控制窗格发送坐标。
Assuming you've got a mapview set up and running:
You'll need the following permission in your manifest:
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.
对于新模拟器:
http://developer.android.com/tools/devices/ emulator.html#extended
基本上,单击模拟器控件中的三个点按钮(位于模拟器右侧),它将打开一个菜单,允许您控制模拟器,包括位置
For the new emulator:
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 用户。
运行模拟器后,转到“工具”->“Android”->“Android 设备监视器”。
从位置控制组中单击“模拟器控制”选项卡更改。
2. Eclipse 用户。
首先在 Eclipse 菜单中选择“Window”,然后选择“Open Perspective”,然后选择“DDMS”。
即窗口->打开透视->DDMS。
您将在左侧设备面板上看到,在右侧您将看到不同的选项卡。
选择“模拟器控制”选项卡。
在底部您将看到位置控制面板。
选择“手动”选项卡。
在文本框中输入经度和纬度,然后单击发送按钮。
它将把位置发送给您的模拟器和应用程序。
3.使用 telnet。
在运行命令中键入此命令。
如果您不使用 Windows,则可以使用任何 telnet 客户端。
使用 telnet 连接后,使用以下命令将您的位置发送到模拟器。
4.使用基于浏览器的 Google 地图工具
有一个程序使用 GWT 和 Google Maps API 来启动基于浏览器的地图工具来在模拟器中设置 GPS 位置:
android-gps-emulator
1. Android Studio users.
After running the emulator goto Tools->Android->Android device monitor
Click the Emulator Control Tab change from the location controls group.
2. Eclipse users.
First In Eclipse In Menu Select "Window" then Select "Open Perspective" then Select "DDMS".
i.e Window->Open Prespective->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.
In the run command type this.
If you are not using windows you can use any telnet client.
After connecting with telnet use the following command to send your position to emulator.
4. Use the browser based Google maps tool
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:
android-gps-emulator
在模拟器控制台中使用“geo”命令
要从命令行发送模拟位置数据:
在 Android 模拟器中启动您的应用程序,然后在 SDK 的 /tools 目录中打开终端/控制台。
连接到模拟器控制台:
telnet localhost 5555
(将 5555 替换为运行模拟器的任何端口)发送位置数据:
* 地理修复以发送固定地理位置。
此命令接受以十进制为单位的经度和纬度,以及以米为单位的可选海拔高度。例如:
Using the "geo" command in the emulator console
To send mock location data from the command line:
Launch your application in the Android emulator and open a terminal/console in your SDK's /tools directory.
Connect to the emulator console:
telnet localhost 5555
(Replace 5555 with whatever port your emulator is running on)Send the location data:
* geo fix to send a fixed geo-location.
This command accepts a longitude and latitude in decimal degrees, and an optional altitude in meters. For example:
我编写了一个 python 脚本,通过 telnet 将 GPS 位置推送到模拟器。它定义了源位置和目标位置。还有一个时间偏移量,可以让您控制将坐标推送到设备的时间长度。每秒推送一次一个位置。
在下面的示例中,脚本在 120 秒内从柏林移动到汉堡。每秒一步/GPS 位置,距离随机。
I wrote a python script to push gps locations to the emulator via telnet. 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. One step/gps location per second with random distances.
在 Linux 中,通信端口被阻止。将终端导航到 android sdk 内的 platform-tools 文件夹并触发以下命令:
In Linux where communication ports are blocked. navigate the terminal to platform-tools folder inside
android sdk
and fire this command:在 Mac、Linux 或 Cygwin 中:
这会将您置于墨西哥城。相应地更改您的经度/纬度/海拔高度。如果您对 nmea 不感兴趣,这应该足够了。
In Mac, Linux or Cygwin:
That will put you in Mexico City. Change your longitude/latitude/altitude accordingly. That should be enough if you are not interested in nmea.
我使用eclipse插件DDMS功能来发送GPS。
I use eclipse plug DDMS function to send GPS.
请参阅获取用户位置
查看提供模拟位置数据。您会找到解决方案。
See Obtaining User Location
Look under Providing Mock Location Data. You will find the solution for it.
首先进入 Eclipse 中的 DDMS 部分
比打开模拟器控制....
转到手册部分
设置纬度和经度,然后按发送按钮
First go in DDMS section in your eclipse
Than open emulator Control ....
Go To Manual Section
set lat and long and then press Send Button
我试图通过 adb 设置许多点的地理修复,但无法让我的应用程序查看任何 GPS 数据。但是当我尝试打开 DDMS、选择应用程序的进程并通过模拟器控制选项卡发送坐标时,它立即起作用了。
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.
Dalvik 调试监视器 >选择模拟器>模拟器控制选项卡>位置控制。
DDMS -- android_sdk/tools/ddms 或 android_sdk/tools/monitor
Dalvik Debug Monitor > Select Emulator > Emulator Control Tab > Location Controls.
DDMS -- android_sdk/tools/ddms OR android_sdk/tools/monitor
如果您使用 Eclipse,则使用模拟器控制器,您可以手动设置纬度和经度并在模拟器中运行基于地图的应用程序
If you are using eclipse then using Emulator controller you can manually set latitude and longitude and run your map based app in emulator
如果您使用的是 Android Studio (1.3):
If you're using Android Studio (1.3):
只需将 Alberto Gaona 的答案写成一行即可
5554 是
adb devices
中显示的模拟器端口号。如果
adb emu
能够工作那就更好了。Just make Alberto Gaona's answer into one line
5554 is the emulator port number shown in
adb devices
.It would have been better if
adb emu
work.转到仿真中的
扩展控件
。之后,您可以通过搜索或将地图拖动到要设置的位置来设置模拟的位置。最后点击
SET LOCATION
按钮保存。Go to
Extended controls
in emulate. 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.如果上述解决方案不起作用。试试这个:
在你的 android Manifest.xml 中,添加以下两个链接 OUTSIDE 应用程序标签,但当然在你的清单标签内
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
当模拟器在没有 Google API 的情况下运行 Android 映像时,我无法在模拟器上修复 GPS。一旦我将图像更改为包含 Google API,此处提到的所有 GPS 修复方法都会起作用。
创建 AVD 时,请确保选择带有 Google API 的图像。
I was unable to get a GPS fix on the emulator when emulator was running Android image without Google APIs. As soon as I changed the image to contain Google APIs all of the here mentioned ways to get a GPS fix worked.
Make sure you select an image with Google APIs when creating AVD.
已经多次提到的使用 shell 命令“geo fix...”的答案是正确的答案。
但如果您使用 LocationClient.getLastLocation() 来检索数据,值得一提的是,它一开始不起作用。 LocationClient 类使用 Google Play 服务来检索坐标。
对我来说,这在运行模拟器地图应用程序一次后开始工作。在第一次启动期间,系统会要求您允许谷歌应用程序访问您的位置,我想这可以解决问题。
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.
对于我自己的一个项目,我开发了一个在线服务,可以为Android模拟器提供模拟位置。
它使用 geo nmea 而不是 geo fix,除了纬度/经度之外,还可以设置速度、航向、精确时间等。
该服务只需要 nc (netcat) 命令行实用程序即可。
http://www.kisstech.ch/nmea/
For a project of my own, I developed an online service which can provide simulated location to the Android emulator.
It uses geo nmea rather than geo fix which allows it to set speed, course, precise time etc. in addition to just lat/lon.
The service requires the nc (netcat) command line utility and nothing else.
http://www.kisstech.ch/nmea/
您可以使用 genymotion 这样的模拟器,它可以让您灵活地模拟当前的 GPS 位置等。
You can use an emulator like genymotion which gives you the flexibility to emulate your present GPS location, etc.
Android Studio 有一个名为“Mock Location Plugin”的插件。
您可以使用此插件模拟多个点。
您可以在此链接中找到详细的使用手册: Android Studio。使用模拟位置插件模拟多个 GPS 点
There is a plugin for Android Studio called “Mock Location Plugin”.
You can emulate multiple points with this plugin.
You can find a detailed manual of use in this link: Android Studio. Simulate multiple GPS points with Mock Location Plugin