如何使用python获取Android中移动设备的当前纬度和经度?

时间:2017-07-31 16:01:28

标签: python bots telegram

我制作了一个脚本python,我希望使用python获取Android中移动设备的当前纬度和经度,我该怎么做,哪个模块更好?

1 个答案:

答案 0 :(得分:1)

如果您需要从已连接的Android设备检索位置信息,您可以使用以下脚本:

import sh
from re import findall
#Location[network 11.111111,11.111111 acc=24
LOCATION_PATTERN ="(\w+)\ (\d+.\d+),(\d+.\d+)\ acc"

def set_location_settings(type, on):
    status = "+" if on else "-"
    sh.adb("shell", "settings", "put", "secure", "location_providers_allowed", "{0}{1}".format(status, type))

def get_location_settings():
    return sh.adb("shell", "settings", "get", "secure", "location_providers_allowed")

def is_location_enabled():
    return bool(get_location_settings().strip())

def get_location_data():
    d = dict()
    for (type, lat, long) in findall(LOCATION_PATTERN, str(sh.adb("shell", "dumpsys", "location"))):
        d.setdefault(type, []).append((lat, long))
    return d

if not is_location_enabled():
    set_location_settings("gps", True)
    set_location_settings("network", True)

print get_location_settings()
print get_location_data()

它使用dumpsys来检索位置信息。此外,它还可以选择启用gps

结果是一个类型字典到lat,long对的列表: 示例输出:

{
 'network': [
    ('11.111111', '11.111111'),
    ('11.111111', '11.111111'),
    ('11.111111', '11.111111'),
 ], 
 'gps': [
    ('11.111111', '11.111111'), 
    ('11.111111', '11.111111'),
    ('11.111111', '11.111111')
  ]
 }