简体   繁体   English

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

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

I need to develop an app for android for a university task and a part of it consists in sending the GPS coords of the device to my database. 我需要为大学任务开发一个Android应用程序,其中一部分是将设备的GPS坐标发送到我的数据库中。 Here's the class that I'm using to get the coords 这是我用来取得协调的班级

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

}

} }

That is called inside InterfaceActivity.java, using this: 使用以下命令在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()));

And finally HttpDevices is an asynttask that "runs" http messages to my database, using this code: 最后,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);

But the coords send to the database are always 0.0 - 0.0 , and I can't even try the code an debug it on the emulator ( blue stack ) because it doesn't have gps coords. 但是发送到数据库的坐标始终为0.0-0.0,并且我什至无法尝试在模拟器(蓝色堆栈)上调试它的代码,因为它没有gps坐标。 Tried it on my Moto G but they are just 0. I tried spamming that string (connessione.php etc.. ) in my broweser giving random values for lat and long and yes, it works. 在我的Moto G上试过,但它们仅为0。我尝试在我的浏览器中对该字符串(connessione.php等)进行垃圾邮件处理,并给出了lat和long的随机值,是的,它可以正常工作。 It updates the database and shows the coords in my app, so the problem should be in the java. 它会更新数据库并在我的应用程序中显示坐标,因此问题应该出在Java中。 Any tips? 有小费吗? I've searched alot in google/stackoverflow and didnt' find anything, it's the first time I encouter a problem that requires me to post here. 我在google / stackoverflow中搜索了很多内容,却没有找到任何东西,这是我第一次遇到一个问题,需要我在这里发布。 Hope you can help me! 希望你能帮我! Thank youu! 谢谢你!

Edit: in case you want to take a look at my whole code there is a github link: https://github.com/leejaku/RemoteAndroid2 编辑:如果您想看一下我的整个代码,则有一个github链接: https : //github.com/leejaku/RemoteAndroid2

I just try to get through your code... Please correct me if I have missed something 我只是尝试通过您的代码...如果我错过了什么,请更正我

If I look at your HttpDevices Class you try to send your Data like this... 如果我查看您的HttpDevices类,您会尝试像这样发送数据...

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

This will try to get the value of a class property which is 0.0, because it is not the instance of MyLocationListener that is updating the lat and long... you need to create a Singleton of MyLocationListener to get one single sharedInstance of your listener, or you have to do it with a Delegate Pattern, or you save the Values to SharedPreferences... 这将尝试获取为0.0的类属性的值,因为它不是更新lat和long的MyLocationListener的实例。您需要创建MyLocationListener的Singleton来获取侦听器的一个sharedInstance,或您必须使用委托模式来执行此操作,或者将值保存到SharedPreferences ...

Do you have enough knowledge of Java to build one of these Paradigms, and does it make sense to you? 您是否有足够的Java知识来构建这些范例之一,这对您有意义吗? (Sorry for that Question, I don't know how good you are mate :-)...) Otherwise I could write you something to solve it (对于这个问题,很抱歉,我不知道你是多么棒的伴侣:-)...)否则,我可以给你写点东西来解决它

UPDATE 更新

Well this problem seems to be nasty, so we will need a "NastyLocationListener" to solve it, and thank Neo has written the ultimate one ;-) 好吧,这个问题似乎很麻烦,因此我们将需要一个“ NastyLocationListener”来解决它,并感谢Neo写了最终的一个;-)

Checkout this Class https://gist.github.com/DennisWeidmann/de2ebb2b2549a938fa50 检出此类https://gist.github.com/DennisWeidmann/de2ebb2b2549a938fa50

Usage just like this 用法就是这样

/////////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

UPDATE 更新

Screenshot of your App on my device without changing any code instead of the REST Call, I have inserted a Log to output my Latitude.. 我的设备上您的应用程序的屏幕快照,未更改任何代码(而不是REST调用),我插入了一个日志以输出我的谷歌纵横。

在此处输入图片说明

Try with this class, it's from my github account: https://github.com/chiiiky14/GPStracker 尝试使用该类,它来自我的github帐户: https : //github.com/chiiiky14/GPStracker

Import it to your project and create the object: 将其导入您的项目并创建对象:

GPSTracker gpstracker = new GPSTracker(context);

And after that: 在那之后:

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

Just make sure the GPS of your device is already activate. 只需确保设备的GPS已经激活即可。 Hope this will help. 希望这会有所帮助。

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

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