Android 应用在 textView 中显示经纬度

Android app showing lat long in textViews

我试图在 textView 上显示纬度和经度,但它似乎不起作用。 lat 和 long 将通过模拟器控件发送,当我尝试发送值时它应该在 textViews 上显示它们但它似乎不起作用,也许我错过了什么?

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");
    }
}

让您的 activity 实施 LocationListener

implements LocationListener

然后在 onCreate

中请求 locationUpdates
lm.requestLocationUpdates(provider, 20000, 0, this);

然后在您为实现 LocationListener 而重写的 onLocationChanged 方法中,在您发送位置后更新您的 textViews

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