我正在尝试获取LatLong并在文本视图中填充它。我使用androidhive的GPSTracker。我的按钮监听器如下。
btnLatLng.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(getBaseContext(),"Clicked!!", Toast.LENGTH_SHORT).show();
//setLatLong();
gps = new GPSTracker(MainActivity.this);
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// \n is for new line
// Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
txtLat.setText("Latitude:"+Double.toString(latitude));
txtLong.setText("Longitude:"+Double.toString(longitude));
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
});
我能够在模拟器中获得lat。在设备中,它显示0.0作为值。