【发布时间】:2018-06-19 16:38:09
【问题描述】:
我的应用获取用户坐标。现在我尝试获取坐标所属城市的名称。我搜索了其他线程,但没有发现任何有用或新的东西。我应该显示地址名称的 textView 只是保持为空,还是获取地址的方法错误?
我的代码:
public class MainActivity extends AppCompatActivity {
double lat;
double lon;
Button btnLoc;
TextView textView7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView7 = (TextView) findViewById(R.id.textView7);
btnLoc = (Button) findViewById(R.id.btnGetLoc);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);
btnLoc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GPSTracker gt = new GPSTracker(getApplicationContext());
Location location = gt.getLocation();
if (location == null) {
Toast.makeText(getApplicationContext(), "GPS unable to get Value", Toast.LENGTH_SHORT).show();
} else {
double lat = location.getLatitude();
double lon = location.getLongitude();
TextView textView5 = (TextView) findViewById(R.id.textView5);
textView5.setText(String.valueOf(lat));
TextView textView6 = (TextView) findViewById(R.id.textView6);
textView6.setText(String.valueOf(lon));
}
}
});
try {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lon, 1);
if (addresses.size() > 0)
textView7.setText(addresses.get(0).getLocality());
} catch (IOException e) {
}
}
}
【问题讨论】:
标签: java android coordinates