在我的项目中,我正在尝试获取纬度,经度和地址但由于某种原因我无法在每次显示消息Location not available
。
在manifest.xml中
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
以前是否有人遇到此问题? 这是我的班级:
public class GEO extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private TextView addressField; //Add a new TextView to your activity_main to display the address
private LocationManager locationManager;
private String provider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.geo);
latituteField = (TextView) findViewById(R.id.txt1);
longitudeField = (TextView) findViewById(R.id.txt2);
addressField = (TextView) findViewById(R.id.txt3);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, true);
Log.d("SomeTag", provider);
Log.d("SomeTag", String.valueOf(locationManager.isProviderEnabled(provider)));
Location location = locationManager.getLastKnownLocation(provider);
String locationProvider = LocationManager.GPS_PROVIDER;
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
if (lastKnownLocation != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(lastKnownLocation);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
addressField.setText("Location not available");
}
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
//You had this as int. It is advised to have Lat/Loing as double.
double lat = location.getLatitude();
double lng = location.getLongitude();
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
StringBuilder builder = new StringBuilder();
try {
List<Address> address = geoCoder.getFromLocation(lat, lng, 1);
int maxLines = address.get(0).getMaxAddressLineIndex();
for (int i=0; i<maxLines; i++) {
String addressStr = address.get(0).getAddressLine(i);
builder.append(addressStr);
builder.append(" ");
}
String fnialAddress = builder.toString(); //This is the complete address.
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
addressField.setText(fnialAddress); //This will display the final address.
} catch (IOException e) {}
catch (NullPointerException e) {}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
我用手机运行我的项目
答案 0 :(得分:1)
可能是
address.get(0)
为空
List<Address> address = geoCoder.getFromLocation(lat, lng, 1);
int maxLines = address.get(0).getMaxAddressLineIndex();
您正在捕捉NPE,因此可能很难确定您未获得预期结果的确切原因。
这样的事情可能有所帮助:
try {
List<Address> addresses = geoCoder.getFromLocation(lat, lng, 1);
Address firstAddress = addresses.get(0);
if(firstAddress != null) {
int maxLines = firstAddress.getMaxAddressLineIndex();
for (int i = 0; i < maxLines; i++) {
String addressStr = firstAddress.getAddressLine(i);
builder.append(addressStr);
builder.append(" ");
}
addressField.setText(builder.toString()); //This will display the final address.
}
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
} catch (IOException e) {
e.printStackTrace();
}
此外,来自http://developer.android.com/reference/android/location/Geocoder.html
尝试检查isPresent
public static boolean isPresent ()
Added in API level 9
Returns true if the Geocoder methods getFromLocation and getFromLocationName are implemented. Lack of network connectivity may still cause these methods to return null or empty lists.