【问题标题】:How to make GPS-app for android using kivy, pyjnius?如何使用 kivy、pyjnius 为 android 制作 GPS 应用程序?
【发布时间】:2013-05-30 19:24:28
【问题描述】:

我是 KIVY、pyjnius 和 python-android 的新手。我需要为 android 制作简单的应用程序,它显示 GPS 坐标。但是,正如我所说,我是 kivy 和 pyforandroid 的新手。有人可以显示/给我示例,它在简单的 kivy-label-widget 中显示我的坐标吗? 非常感谢!

我试过做这样的事情,但是......

    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()

【问题讨论】:

    标签: android python kivy


    【解决方案1】:

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

    它支持 GPS。

    【讨论】:

      【解决方案2】:

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

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

      看源码,什么都在里面。

      【讨论】:

      • 有趣。我会看看它!目前对 Kivy 中 android 集成的深度感到好奇
      【解决方案3】:

      Plyer 是一个 Python 库,用于访问您的硬件/平台的功能。

      刚找到,所以不太了解。

      文档:

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

      来源:

      https://github.com/kivy/plyer

      在这个 Kivy Github 问题上找到它:

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

      【讨论】:

        【解决方案4】:

        您可以使用另一种替代方法,即Android 模块,请记住,在您的 android 设备中的 Qpython3 中尝试它,因为它具有必要的权限并且还打开了您的 gps 位置。这是代码:

        import android
        droid = android.Android()
        loc = droid.readLocation()
        lattitude = loc.result['network']['latitude']
        longitude = loc.result['network']['longitude']
        print(lattitude, longitude )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-04
          • 2021-06-13
          • 2014-02-26
          • 1970-01-01
          • 1970-01-01
          • 2021-11-01
          • 2012-12-26
          • 1970-01-01
          相关资源
          最近更新 更多