Python开发安卓-Virgo开发日记(4)--增加位置获取支持

文章介绍了Android中的LocationManager模块,包括GPS_PROVIDER、PASSIVE_PROVIDER、FUSED_PROVIDER和NETWORK_PROVIDER等不同位置提供源的特性和用法。通过VLocationListener设置位置变化监听,并展示了如何在实际操作中请求和更新位置信息,强调了需在获取权限后调用相关方法的重要性。
摘要由CSDN通过智能技术生成

em,这个功能其实写起来挺麻烦的....但是还是写了。

顾名思义,就是可以通过GPS/WIFI/Passive获取当前位置,还可以设置精确度什么的

不多BB,直接上介绍

【壹】功能介绍

新增模块:

Virgo.android.core_components.LocationManager

模块内常用类、函数
类名介绍内部常用函数、类变量
VLocationManager
位置管理器,内含位置提供源的类变量,以及获取位置、更新位置对象的方法
GPS_PROVIDER:GPS提供位置,就是全球卫星导航系统,卫星定位模式、定位精度最高(10米)、能够获取海拔高度、耗电量大、室内无法定位。
PASSIVE_PROVIDER被动接收更新地理位置信息,该应用接收到消息后直接读取就可以了。比如如果系统中已经安装了百度地图,高德地图。
FUSED_PROVIDER:【不用】Google api,API容易使用、定位精度高、根据电量自动选择定位模式、Google的融合定位服务天朝不能用,不需要明确指定定位模式,由服务提供者自动判断,给出最佳的定位方式,需要GoogleAPI,Google Play Services
NETWORK_PROVIDER :网络提供位置,通过WIFI或者热点大致判断你处在什么位置。网络定位模式(基站、wifi网络)、精度较低(1000米)、无海拔高度、初始定位速度快、耗电低。
---------------
get_all_providers:获取全部可用的位置提供者
getLastKnownLocation:获取最新的、最后一次获取的位置对象
requestLocationUpdates:请求、更新一次位置,返回位置对象

VLocationListener
用于设置:当位置改变时、当位置提供源改变时、当精度改变时等分别调用的函数
setLocationChanged(func(location)):位置改变
setProviderDisabled(func(provider)):位置提供源不可用了
setProviderEnabled(func(provider)):位置提供源可用了
setStatusChanged(func(provider,status,extras)):当精度改变了
VLocation
那个“一直被返回的”位置对象
get_longitude():获取经度
get_latitude():获取纬度
get_accuracy():获取精度
get_altitude():获取高度
get_bearing():获取方向
get_speed():获取速度

【贰】实际操作

导入需要用到的类

from Virgo.android.core_components.LocationManager import VLocationListener,VLocationManager, VLocation

 1.获取位置管理器(无需自己定义)

Activity内部提供一个方法:self.get_location_manager() 即可获取LocationManager

2.位置管理器.requestLocationUpdates(参数1,参数2,参数3,参数4)

此方法用于注册 获取位置对象后 的操作

▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋

【☢】  但注意:此方法必须在位置权限获取后调用,或在权限判断的安全区内调用(不知道怎样获取权限的请回顾日记(3))

▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋ ▋

参数1:位置提供源,例如VLocationManager.GPS_PROVIDER

参数2:更新时间间隔(毫秒)

参数3:空间距离位置间隔(米)

参数4:vlocation_listener:VLocationListener对象

 3.创建:VLocationListener

vll = VLocationListener()
vll.setLocationChanged(func)

注意:func所对应的自定义函数体,必须有一个参数location用于获取“返回的位置对象Vlocation”,详细方法请参照上面的VLocation介绍

【叁】源代码

class LocationActivity(ControlActivity):
    def __init__(self, APP):
        super().__init__("TryLocation", APP)
        self.providers_text = None
        self.information_text = None
        self.try_button = None

    def update_location(self, location: VLocation):
        information_dict = {
            "经度:": location.get_longitude(),
            "纬度:": location.get_latitude(),
            "精确度:": location.get_accuracy(),
            "高度:": location.get_altitude(),
            "方向:": location.get_bearing(),
            "速度:": location.get_speed()
        }

        self.information_text.setText(str(information_dict))

    def onCreate(self, savedInstanceState):
        self.setContentView("locationlay")
        self.providers_text = self.findViewById("position_providers")
        self.information_text = self.findViewById("position_information_show")
        self.try_button = self.findViewById("button_try_location")

        # 获取位置提供源
        self.providers_text.setText(str(self.get_location_manager().get_all_providers()))

        self.try_button.setOnClickListener(
            VOnClickListener().register_onClick(
                lambda view: self.APP.change_activity("RequestsTry")
            ))

        # -----------
        if check_activity_permission(
                [PermissionManager.ACCESS_COARSE_LOCATION, PermissionManager.ACCESS_FINE_LOCATION],
                self):
            self.get_location_manager().requestLocationUpdates(VLocationManager.GPS_PROVIDER,
                                                               1000,
                                                               1,
                                                               VLocationListener().setLocationChanged(
                                                                   lambda
                                                                       location: self.update_location(
                                                                       location)))

            self.update_location(
                self.get_location_manager().getLastKnownLocation(VLocationManager.GPS_PROVIDER))
        else:
            ask_for_permission(self, "请同意请求", 666, [PermissionManager.ACCESS_COARSE_LOCATION,
                                                         PermissionManager.ACCESS_FINE_LOCATION])

    def onPermissionsGranted(self, requestCode, permissions):
        toast(self, "权限获取成功!")
        self.get_location_manager().requestLocationUpdates(VLocationManager.GPS_PROVIDER,
                                                           1000,
                                                           1,
                                                           VLocationListener().setLocationChanged(
                                                               lambda
                                                                   location: self.update_location(
                                                                   location)))

        self.update_location(
            self.get_location_manager().getLastKnownLocation(VLocationManager.GPS_PROVIDER))

    def onPermissionsDenied(self, requestCode, permissions):
        toast(self, "权限获取失败..")

    def onRationaleAccepted(self, requestCode):
        toast(self, "点击了确定按钮......")

    def onRationaleDenied(self, requestCode):
        toast(self, "点击了取消按钮")

【肆】运行效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值