2015-12-10 65 views
4

我試圖在textView上顯示經度和緯度,但它似乎不工作。 lat和long將通過模擬器控件發送,當我嘗試發送值時,它應該在textViews上顯示它們,但它似乎不起作用,也許我錯過了某些東西?Android應用程序在文本視圖中顯示經度和緯度

public class MainActivity extends Activity implements View.OnClickListener{ 

//Defining views 
private EditText editTextLatitude; 
private EditText editTextLongitude; 
private EditText editTextTimeInserted; 
LocationManager lm; 
TextView lt, ln; 
String provider; 
Location l; 
private Button buttonAdd; 
private Button buttonView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Initializing views 
    editTextLatitude = (EditText) findViewById(R.id.editTextLat); 
    editTextLongitude = (EditText) findViewById(R.id.editTextLon); 
    editTextTimeInserted = (EditText) findViewById(R.id.editTextTimeInserted); 

    buttonAdd = (Button) findViewById(R.id.buttonAdd); 
    buttonView = (Button) findViewById(R.id.buttonView); 

    //Setting listeners to button 
    buttonAdd.setOnClickListener(this); 
    buttonView.setOnClickListener(this); 
    ln = (TextView) findViewById(R.id.lng); 
    lt = (TextView) findViewById(R.id.lat); 
    lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
    Criteria c = new Criteria(); 

    provider = lm.getBestProvider(c, false); 

    l = lm.getLastKnownLocation(provider); 

    if (l != null) {  //get latitude and longitude of the location 
     double lng = l.getLongitude(); 
     double lat = l.getLatitude(); 
     //display on text view 
     ln.setText("" + lng); 
     lt.setText("" + lat); 
    } else { 
     ln.setText("No Provider"); 
     lt.setText("No Provider"); 
    } 
} 
+0

stacktrace?請打印 – therealprashant

+0

意思是,當你改變latlng的時候,價值沒有變化? –

+0

是的lng和緯度textViews沒有改變,他們是假設改變任何拉特長在模擬器控制.. @NigamPatro – Tuks

回答

1

有你的活動實施LocationListener的

implements LocationListener 

然後請求裏面locationUpdates您的onCreate

lm.requestLocationUpdates(provider, 20000, 0, this); 

然後在你重寫實施LocationListener的更新textViews一旦你發送onLocationChanged方法位置

@Override 
public void onLocationChanged(Location location) { 
    double lng = location.getLongitude(); 
    double lat = location.getLatitude(); 
    //display on text view 
    ln.setText("" + lng); 
    lt.setText("" + lat);  
} 
+0

是的,現在完美的工作,謝謝@JRowan :) ..當我在我的活動中實現LocationListener與我的按鈕實現View.OnClickListener時,它提供了一個錯誤類活動必須聲明抽象實現抽象方法onProviderEnabled(String ) – Tuks

+0

我認爲它說你從LocationListener實現其餘的方法,我不認爲你必須但是爲了讓你的代碼完整,你可能想要的,然後我可能是錯的,我從來沒有這個問題 – JRowan

+0

Iv整理它,它告訴我從LocationListener實現其餘的方法。謝謝你的幫忙@JRowan – Tuks

相關問題