Android 应用程序,展示 lat 长在 textViews

标签: Android
发布时间: 2016/9/26 4:26:44
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想显示的纬度和经度 textView 但这似乎并不工作。当我尝试发送它应该在 textViews,但它显示它们的值似乎不工作,也许我错过了什么东西时,将通过仿真程序控制,发送的 lat 和长吗?

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

解决方法 1:

有你 activity 执行LocationListener

implements LocationListener

然后请求你 onCreate 里面 locationUpdates

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

然后在你 onLocationChanged 实现的方法重写 LocationListener 更新你的 textViews,一旦你发送location

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