在 Android 应用程序中使用 LocationListener,GPS 坐标始终为 0.0-0.0

GPS Coords being always 0.0-0.0 using LocationListener in a Android App

我需要为大学任务 android 开发一个应用程序,其中一部分是将设备的 GPS 坐标发送到我的数据库。这是我用来获取坐标的 class

public class MyLocationListener implements LocationListener {

double latitude;
double  longitude;


public MyLocationListener(Context myContext){
    LocationManager locationManager = (LocationManager) myContext.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

public  double getLatitude(){
    return latitude; }

public  double getLongitude(){
    return longitude; }

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

   latitude =  location.getLatitude();
    longitude =  location.getLongitude();

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}

就是在InterfaceActivity.java里面调用的,用这个:

        MyLocationListener ggwp = new MyLocationListener(InterfaceActivity.this);
    HttpDevices elenco = new HttpDevices(LoginIstance.getIst().getLog()[0],LoginIstance.getIst().getLog()[1],LoginIstance.getIst().getID(),String.valueOf(ggwp.getLatitude()),String.valueOf(ggwp.getLongitude()));

最后,HttpDevices 是一个异步任务,它 "runs" 将 http 消息发送到我的数据库,使用此代码:

            HttpPost httppost = new HttpPost("http://88.116.86.82/android/remote/connessione.php?user="+usr+"&pass="+pss+"&id="+id+"&lat="+lat+"&long="+log);

但是发送到数据库的坐标始终是 0.0 - 0.0,我什至无法尝试在模拟器 (blue stack) 上调试代码,因为它没有 gps 坐标。在我的 Moto G 上试过,但它们只是 0。我尝试在我的浏览器中发送该字符串(connessione.php 等),为纬度和经度提供随机值,是的,它有效。它更新数据库并在我的应用程序中显示坐标,所以问题应该在 java 中。有小费吗?我在 google/Whosebug 中搜索了很多但没有找到任何东西,这是我第一次遇到需要我在这里 post 的问题。希望你能帮我!谢谢你!

编辑:如果您想查看我的整个代码,可以使用 github link: https://github.com/leejaku/RemoteAndroid2

我只是想通过你的代码...如果我遗漏了什么请纠正我

如果我查看你的 HttpDevices Class 你尝试像这样发送你的数据...

lat = String.valueOf(MyLocationListener.latitude);
log = String.valueOf(MyLocationListener.longitude);

这将尝试获取 class 属性 的值为 0.0,因为更新纬度和经度的不是 MyLocationListener 的实例...您需要创建一个 MyLocationListener 的 Singleton 以获得您的侦听器的一个 sharedInstance,或者您必须使用委托模式来完成它,或者您将值保存到 SharedPreferences...

您是否有足够的 Java 知识来构建这些范例之一,它对您有意义吗? (很抱歉这个问题,我不知道你有多好 :-)...)否则我可以给你写一些东西来解决它

更新

好吧,这个问题似乎很讨厌,所以我们需要一个 "NastyLocationListener" 来解决它,感谢 Neo 写了最终的 ;-)

检查这个 Class https://gist.github.com/DennisWeidmann/de2ebb2b2549a938fa50

用法就是这样

/////////Just to know it
        Log.e("lati", NastyLocationListener.getLatitude(this)); //On most phones you can use the Listener without any other Code
        Log.e("longi", NastyLocationListener.getLongitude(this)); //It uses Androids native cached location but this is not updated proper
        /////////Just to know it end

        /////////Normal usage
        nastyLocationListener = new NastyLocationListener(this); //In your MainActivity instanciate a NastyLocationListener (its only needed once... All other Activities could get data from him)
        nastyLocationListener.startListening(); //It starts listening to Location Updates and triggers the Location Provider to fetch updates (its bool, true if almost one Provider is available, false if no Provider is available)

        nastyLocationListener.stopListening(); //Use this line when you pause the app, or quit it... it will stop the listener and could be resumed with startListening()

        NastyLocationListener.getLatitude(this); //Could be used in every class, on every time, however you want without any other code like above
        NastyLocationListener.getLongitude(this); //Could be used in every class, on every time, however you want without any other code like above
        /////////Normal usage end

更新

你的应用程序在我的设备上的屏幕截图,没有更改任何代码而不是 REST 调用,我插入了一个日志来输出我的 Latitude..

试试这个 class,它来自我的 github 帐户:https://github.com/chiiiky14/GPStracker

将其导入您的项目并创建对象:

GPSTracker gpstracker = new GPSTracker(context);

之后:

  gpstracker.getLatitude();
  gpstracker.getLongitude();

只需确保您设备的 GPS 已激活。希望这会有所帮助。