Qpython SL4A服务调用GPS定位获取获取位置信息

代码很简单,由于网上缺乏教程,在这里我做个简单分享。
1.调用gps
2.获取经纬度,以及高程等信息。
3.实用高德逆地理查询API,查询位置。
4.调用安卓系统自带语音模块,朗读位置。

# -*- coding: UTF-8 -*-
import json,requests
import androidhelperdroid = androidhelper.Android()
droid.startLocating()
requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = "TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-256-GCM-SHA384:ECDHE:!COMPLEMENTOFDEFAULT"
Gx = ""
Gy=""while 1:    
    gpsdata = droid.readLocation().result         
    if len(gpsdata)>0:        
    if gpsdata['network']:            
    x = str(gpsdata['network']['longitude'])            
    y = str(gpsdata['network']["latitude"])            
    Gx+=x; Gy+=y            #print (gpsdata['network']) #取得Gps导向(bearing)(角度)            
    break;
droid.stopLocating()
#--------------------def 
parse(longitude,latitude):    
key = '去高德注册一个api,填写自己的key值'    
location = f'{longitude},{latitude}'    
radius = '200'    
url = f'https://restapi.amap.com/v3/geocode/regeo?output=json&location={location}&key={key}&radius={radius}&extensions=all'    
# 默认使用get方法请求    
Msg =""    
res = requests.get(url)    
formatted_address = res.json()['regeocode']['formatted_address']    
roads = res.json()['regeocode']['roads']    print(formatted_address)    
for i in roads:        
    Msg+=i['name']    
droid.ttsSpeak("您所处的位置为:"+ Msg)         
print(Msg)
print( Gx, Gy)parse(Gx,Gy)

猜你喜欢

转载自blog.csdn.net/qq_17802895/article/details/113063212