簡體   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