提问人:Brad 提问时间:11/11/2023 更新时间:11/12/2023 访问量:45
为什么我的 Kivy 应用程序没有从 Android 获得纬度和纬度?
Why is my Kivy application not getting lat and lon from Android?
问:
我正在编写一个 Android 应用程序,它可以根据用户的位置从最近的气象站获取天气。我无法让 Kivy 从用户的手机中获取位置。下面是我的代码。
我在 buildozer.spec 文件中设置了适当的权限来请求 GPS 位置。
我的 buildozer.spec 文件中的权限 android.permissions = 互联网、ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION
import requests
from kivy.uix.textinput import TextInput
from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.dialog import MDDialog
from kivy.uix.image import Image
from kivymd.uix.button import MDRectangleFlatButton
from kivymd.uix.toolbar import MDTopAppBar
from kivy.utils import platform
KV = '''
MDBoxLayout:
orientation: 'vertical'
MDTopAppBar:
title: "Wing Calculator"
right_action_items: [["rotate-3d-variant", lambda x: app.clear_inputs()]]
MDLabel:
id: conditions_label
text: "Current Conditions"
halign: "center"
valign: "middle"
theme_text_color: "Secondary"
MDTextField:
id: weight_input
hint_text: "Weight"
helper_text: "Enter weight in pounds"
helper_text_mode: "persistent"
pos_hint: {"center_x": 0.5}
size_hint_x: None
width: "200dp"
MDLabel:
text: "Beginner"
halign: "center"
MDSwitch:
group: "skill"
id: skill_switch
value: "Beginner"
pos_hint: {"center_x": 0.5}
MDLabel:
text: "Small Wing Preferred"
halign: "center"
MDSwitch:
group: "style"
id: style_switch
value: "Less power"
pos_hint: {"center_x": 0.5}
MDRaisedButton:
text: "Calculate"
on_release: app.wing_calculator()
pos_hint: {"center_x": 0.5}
'''
class WingCalculatorApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.winddirection = None
self.windgust = None
self.wind = None
self.locationname = None
self.lon = None
self.lat = None
def build(self):
self.theme_cls.primary_palette = 'BlueGray'
self.theme_cls.primary_hue = 'A700'
self.theme_cls.theme_style = 'Light'
return Builder.load_string(KV)
def clear_inputs(self):
# Access the root widget or any other widget that contains the text inputs
root_widget = self.root
# Find all text input widgets using the `walk` method
text_inputs = [widget for widget in root_widget.walk() if isinstance(widget, TextInput)]
# Clear the text in each text input widget
for text_input in text_inputs:
text_input.text = ''
def on_start(self):
self.runGPS()
self.gps_location()
self.get_wind()
self.currentconditions()
self.display_conditions()
def runGPS(self):
if platform == 'android':
from android.permissions import Permission, request_permissions
def callback(permission, results):
if all([res for res in results]):
print("Got all permissions")
from plyer import gps, android
gps.configure(on_location=self.gps_location,
on_status=self.on_auth_status)
gps.start(minTime=1000, minDistance=0)
else:
print("Did not get all permissions")
request_permissions([Permission.ACCESS_COARSE_LOCATION,
Permission.ACCESS_FINE_LOCATION], callback)
def gps_location(self, *args, **kwargs):
self.lat = kwargs.get('lat')
self.lon = kwargs.get('lon')
print(f"GPS position {self.lat}, {self.lon}")
if self.lat is None and self.lon is None:
self.lat = 21.5155198
self.lon = -87.6696238
print("Latitude and longitude not found in GPS data.")
return self.lat, self.lon
def on_auth_status(self, general_status, status_message):
if general_status == 'provider-enabled':
pass
else:
self.open_gps_access_popup()
def open_gps_access_popup(self):
dialog = MDDialog(title="GPS Error", text="You need to enable GPS access for the app to function properly")
我必须添加更多文本,因为我的帖子主要是代码。
答: 暂无答案
评论