如何使用kivy,pyjnius为android做GPS应用程序?

Im在KIVY,pyjnius和pythonandroid中都是新的。 我需要为android,显示GPS坐标简单的应用程序。 但是,正如我所说,我是新的基维和pyforandroid。 有人可以显示/给我例子,它显示了我的坐标在简单的kivy标签小部件? 非常感谢!

我曾试图做这样的事情,但…

package org.renpy.android; //import java.util.ArrayList; //import java.util.List; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.content.Context; import android.os.Bundle; import android.os.Looper; import java.lang.Thread; import android.app.Activity; public class KivyGps { LocationManager lm; Thread gpsThread; public long minDistance = 1; public int minTime = 1000; static class KivyLocationListener implements LocationListener { public Location lastLocation = new Location("Other"); //private List<LocationListener> listeners = new ArrayList<LocationListener>(); public void onLocationChanged(Location location) { // TODO Auto-generated method stub lastLocation = location; //updateListeners(location); } public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } // TODO Auto-generated method stub return lastLocation; } } static public KivyLocationListener locationListener = new KivyLocationListener(); public Thread init(final Activity currActivity) { gpsThread = new Thread( new Runnable() { public void run() { try { Looper.prepare(); lm = (LocationManager) currActivity.getSystemService( Context.LOCATION_SERVICE ); lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, minTime, minDistance, locationListener ); Looper.loop(); } catch ( Exception e ) { e.printStackTrace(); } } } ); return gpsThread; } //gpsThread.start(); } 

在python中

 from jnius import autoclass LocationListener = autoclass('android.location.LocationListener') LocationManager = autoclass('android.location.LocationManager') LocationProvider = autoclass('android.location.LocationProvider') Location = autoclass('android.location.Location') Looper = autoclass('android.os.Looper') Context = autoclass('android.content.Context') KivyGps = autoclass('org.renpy.android.KivyGps') currentActivity = cast('android.app.Activity', PythonActivity.mActivity) lm = currentActivity.getSystemService( Context.LOCATION_SERVICE) if lm.isProviderEnabled( LocationManager.GPS_PROVIDER ): print 'CON GPS' else: print 'SIN GPS' lps = lm.getAllProviders() for lp in lps.toArray(): print lp #Arreglar problema de derechos ACCESS_FINE_LOCATION en Kivy Launcher lp = lm.getProvider('gps') ll = KivyGps.locationListener kgps = KivyGps() gpsThread = kgps.init( currentActivity ) gpsThread.start() loc = ll.getCurrentLocation() if loc: print loc.getLatitude() print loc.getLongitude() 

Solutions Collecting From Web of "如何使用kivy,pyjnius为android做GPS应用程序?"

我之前做过一个在Kivy / pyjnius中访问GPS的演示:

https://github.com/tito/android-demo

看看源代码,一切都在里面。

您现在可以使用Plyer: http ://github.com/kivy/plyer。

它支持GPS。

Plyer是一个用于访问硬件/平台function的Python库。

刚刚find它,所以我不太了解它。

文档:

https://plyer.readthedocs.org/en/latest/

资源:

https://github.com/kivy/plyer

在这个Kivy Github问题上发现它:

https://github.com/kivy/kivy/issues/994