安卓定位:具体位置显示
由于只看经纬度我们无法一下子知道自己的具体位置,所以引入具体位置显示。只需要修改MainActivity.java即可。
修改部分:
(1)、先是调用了setIsNeedAddress传入true,表示我们需要获取当前具体的位置
private void initLocation() { LocationClientOption option = new LocationClientOption(); option.setScanSpan(5000); // option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors); option.setIsNeedAddress(true); mLocationClient.setLocOption(option); }
(2)、调用getProvince、getCity等函数获得具体的位置信息。
public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(final BDLocation location) { runOnUiThread(new Runnable() { @Override public void run() { currentPosition = new StringBuilder(); currentPosition.append("纬度:").append(location.getLatitude()).append(" "); currentPosition.append("经度:").append(location.getLongitude()).append(" "); currentPosition.append("国家:").append(location.getCountry()).append(" "); currentPosition.append("省:").append(location.getProvince()).append(" "); currentPosition.append("市:").append(location.getCity()).append(" "); currentPosition.append("区:").append(location.getDistrict()).append(" "); currentPosition.append("街道:").append(location.getStreet()).append(" "); currentPosition.append("定位方式:"); if (location.getLocType() == BDLocation.TypeGpsLocation) { currentPosition.append("GPS"); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) { currentPosition.append("网络"); } positionText.setText(currentPosition); } }); } }