我已完成以下操作以获取当前位置。但有时getAddressLine()方法给我null值。这种情况非常罕见。但我想永久地解决它。任何人都可以给我一个解决方案!
LocationManager locationManager;
String provider;
Location location;
double lat;
double lng;
String address;
String city;
// How many Geocoder should return our GPSTracker
int geocoderMaxResults = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home, container, false);
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
statusBar = (FrameLayout) getActivity().findViewById(R.id.statusBar);
((MainActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(true);
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
address = getAddressLine(getActivity());
city = getLocality(getActivity());
((MainActivity) getActivity()).getSupportActionBar().setTitle(address + ", " + city);
} else {
Toast.makeText(getActivity(), "Impossible to connect to Geocoder !!",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onLocationChanged(Location location) {
lat = (double) location.getLatitude();
lng = (double) location.getLongitude();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void onProviderEnabled(String s) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void onProviderDisabled(String s) {
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Get list of address by latitude and longitude
* @return null or List<Address>
*/
public List<Address> getGeocoderAddress(Context context) {
if (location != null) {
Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
try {
/**
* Geocoder.getFromLocation - Returns an array of Addresses
* that are known to describe the area immediately surrounding the given latitude and longitude.
*/
List<Address> addresses = geocoder.getFromLocation(lat, lng, this.geocoderMaxResults);
return addresses;
} catch (IOException e) {
e.printStackTrace();
// Log.e(TAG, "Impossible to connect to Geocoder", e);
}
}
return null;
}
/**
* Try to get AddressLine
* @return null or addressLine
*/
public String getAddressLine(Context context) {
List<Address> addresses = getGeocoderAddress(context);
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
String addressLine = address.getAddressLine(geocoderMaxResults);
return addressLine;
} else {
return null;
}
}
public String getLocality(Context context) {
List<Address> addresses = getGeocoderAddress(context);
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
String locality = address.getLocality();
return locality;
}
else {
return null;
}
}
答案 0 :(得分:0)
每个地点可能没有地址。如果您确定您的位置有地址且您的地理编码器无法提供,请重新启动手机并重试(我发现此解决方案没有真正的解决方案,尽管如您所说,这种情况非常罕见)。您必须提供“else”块才能在这些特殊情况下执行某些操作。
答案 1 :(得分:0)
使用下面的代码获取用户的当前位置。这是Google最新的API(日期:2016年1月28日)。
confirm('...');