我已完成以下操作以獲取當前位置。但有時getAddressLine()方法給我空值。這種情況非常罕見。但我想永久解決它。任何人都可以給我一個解決方案!Android中的當前位置
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;
}
}
你必須處理它。並非每個地點都有地址。 – Henry
但我在這裏有一個問題。爲什麼地址大部分時間都是使用相同的代碼?有時它不會。我已經測試過它一次又一次地在同一個位置。 –