繁体   English   中英

在Android应用中使用LocationListener将GPS坐标始终设置为0.0-0.0

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

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

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是一个异步任务,使用以下代码“运行” 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,并且我什至无法尝试在模拟器(蓝色堆栈)上调试它的代码,因为它没有gps坐标。 在我的Moto G上试过,但它们仅为0。我尝试在我的浏览器中对该字符串(connessione.php等)进行垃圾邮件处理,并给出了lat和long的随机值,是的,它可以正常工作。 它会更新数据库并在我的应用程序中显示坐标,因此问题应该出在Java中。 有小费吗? 我在google / stackoverflow中搜索了很多内容,却没有找到任何东西,这是我第一次遇到一个问题,需要我在这里发布。 希望你能帮我! 谢谢你!

编辑:如果您想看一下我的整个代码,则有一个github链接: https : //github.com/leejaku/RemoteAndroid2

我只是尝试通过您的代码...如果我错过了什么,请更正我

如果我查看您的HttpDevices类,您会尝试像这样发送数据...

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

这将尝试获取为0.0的类属性的值,因为它不是更新lat和long的MyLocationListener的实例。您需要创建MyLocationListener的Singleton来获取侦听器的一个sharedInstance,或您必须使用委托模式来执行此操作,或者将值保存到SharedPreferences ...

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

更新

好吧,这个问题似乎很麻烦,因此我们将需要一个“ NastyLocationListener”来解决它,并感谢Neo写了最终的一个;-)

检出此类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调用),我插入了一个日志以输出我的谷歌纵横。

在此处输入图片说明

尝试使用该类,它来自我的github帐户: https : //github.com/chiiiky14/GPStracker

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

GPSTracker gpstracker = new GPSTracker(context);

在那之后:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM