2014-01-25 62 views
0

我正在開發一個Android應用程序,該應用程序在單擊按鈕時獲取當前位置,然後在您單擊另一個按鈕時打印它。Android地理位置打印經緯度

現在,我已經完成了所有的代碼,但我陷入瞭如何檢索經緯度信息來打印它,因爲我在另一個函數上獲得它。

我會後我下面的代碼來解釋我的意思在一個更清晰的方式:

package com.example.geolocation; 

import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.location.LocationProvider; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity implements OnClickListener, LocationListener { 

    private LocationManager locationManager; 
    public String provider; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button start = (Button)findViewById(R.id.start); 
     start.setOnClickListener(this); 
     Button stop = (Button)findViewById(R.id.stop); 
     stop.setOnClickListener(this); 
     Button show = (Button)findViewById(R.id.show); 
     show.setOnClickListener(this); 
     TextView location = (TextView)findViewById(R.id.location); 
    TextView providers = (TextView)findViewById(R.id.providers); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     provider = locationManager.getBestProvider(criteria, false); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 


     switch(v.getId()){ 

     //funcions boto start 
     case R.id.start: 
     locationManager.requestLocationUpdates(
       provider, 
       10000, //temps em ms (10s) 
       500, //distancia (meters) 
       this); 
     //mostrar provider 
     TextView location = (TextView)findViewById(R.id.location); 
     location.setText("Provider: " + provider); 
     break; 

     //funcio boto stop 
     case R.id.stop: 
     locationManager.removeUpdates(this); 
     break; 

     //funcio boto show 
     case R.id.show: 
     Location locations = locationManager.getLastKnownLocation(provider); 
     locationManager.requestLocationUpdates(
       provider, 
       10000, //temps em ms (10s) 
       1000, //distancia (meters) 
       this); 
     //show long & lat 
     TextView providers = (TextView)findViewById(R.id.providers); 
     providers.setText("Latitude & longitude: " + ""); //com traslladar valor loc aqui? 

     break; 
     } 

    } 

    @Override 
    public void onLocationChanged(Location loc) { 
     // TODO Auto-generated method stub 
     int lat = (int) (loc.getLatitude()); 
     int lng = (int) (loc.getLongitude()); 
    loc.toString(); 
    } 

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

    } 

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

    } 

    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
     // TODO Auto-generated method stub 

    } 

} 

從哪裏獲得所需信息的路段是這一個:

@Override 
    public void onLocationChanged(Location loc) { 
     // TODO Auto-generated method stub 
     int lat = (int) (loc.getLatitude()); 
     int lng = (int) (loc.getLongitude()); 
    loc.toString(); 
    } 

,你可以看,我把信息變成一個字符串。

現在,我的問題是:如何在主函數中調用此信息來打印它?在這一細分市場:

//funcio boto show 
     case R.id.show: 
     Location locations = locationManager.getLastKnownLocation(provider); 
     locationManager.requestLocationUpdates(
       provider, 
       10000, //temps em ms (10s) 
       1000, //distancia (meters) 
       this); 
     //show long & lat 
     TextView providers = (TextView)findViewById(R.id.providers); 
     providers.setText("Latitude & longitude: " + ""); //com traslladar valor loc aqui? 

     break; 
+0

你可以讓你的lat-long變量爲全局變量,這樣你就可以在你的課程中的任何地方使用它們。 –

+0

你想把這個信息?關於經度和緯度? –

+0

在一個名爲「providers」的TextView中:你會看到「providers.setText(」Latitude&longitude:「+」「);」的行,對吧? 「+」號之後的「」是一個佔位符,然後我可以放置一個變量來保存該信息。 – user3082271

回答

1

你必須做的是以下幾點:

月1日 - 來聲明一個String來存儲您的lattitude和經度:

private String lattitude , longitude; 

2日 - 在你的GET onLocationChanged做如下:

@Override 
    public void onLocationChanged(Location loc) { 
     // TODO Auto-generated method stub 
     int lat = (int) (loc.getLatitude()); 
     int lng = (int) (loc.getLongitude()); 
     lattitude = "lattitude = "+ lat ; 
     longitude = "longitude = "+ lng; 
    } 

第3次 - 使用經度和緯度在任何地方打印它們 你要 。

希望有所幫助。

+0

它有很多幫助:非常感謝您的幫助。 – user3082271

相關問題