我想在Android模拟器中获得经度和纬度进行测试。
有谁能指导我如何实现这个目标吗?
如何将模拟器的位置设置为测试位置?
我想在Android模拟器中获得经度和纬度进行测试。
有谁能指导我如何实现这个目标吗?
如何将模拟器的位置设置为测试位置?
当前回答
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.
其他回答
我已经做了一个类似于前面的答案之一的小脚本,但使用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"
示例用法:sendloc 5554 2.35607 48.8263
您可以通过Telnet连接到模拟器。然后你有一个模拟器控制台,允许你输入特定的数据,如地理定位,网络等。
如何使用控制台在这里有详细的解释。 要连接到控制台,打开命令行并键入
telnet localhost 5554
然后,您可以使用geo命令在设备上设置纬度、经度以及(如果需要的话)海拔高度,该设备将通过gps位置提供程序传递给所有程序。请参阅上面的链接以获得进一步的说明。
要在控制台中运行的特定命令为
geo fix <longitude value> <latitude value>
我发现这个网站对于找到一个现实的纬度/lng很有用:http://itouchmap.com/latlong.html
如果您需要多个坐标,您也可以使用带有路由的kml文件,本文对此进行了一些描述。我现在找不到更好的消息来源了。
对于新的模拟器:
http://developer.android.com/tools/devices/emulator.html#extended
基本上,点击模拟器控件中的三个点按钮(模拟器的右侧),它将打开一个菜单,允许你控制模拟器,包括位置
如果你正在使用eclipse,然后使用模拟器控制器,你可以手动设置纬度和经度,并在模拟器中运行基于地图的应用程序
还不能评论,所以更新@ectomorphs回答在这里,当telneting现在需要一个认证令牌。在linux中,它在/home/username/.emulator_console_auth_token下
#!/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()
在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