>我正在尝试获取位置名称或地址。我已经通过谷歌融合位置API成功获取了纬度和经度。
现在我想使用纬度和经度获取位置地址(例如:城市名称、路号或特定地址(。
为此,我正在使用谷歌地理编码器,它工作正常。但在某些设备中,位置地址返回空值。
我在网上搜索了这个问题的解决方案,发现一些设备制造商没有在其设备中包含此功能。这就是为什么这些设备无法通过反向地理编码方法找到地址的原因。这是该信息的链接
那么有没有其他方法可以在没有地理编码的情况下将地址名称作为字符串查找?
这是我通过地理编码获取地址的代码
public static void getAddress(Context context, double LATITUDE, double LONGITUDE) {
try {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null && addresses.size() > 0) {
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL
Log.d(TAG, "getAddress: address" + address);
Log.d(TAG, "getAddress: city" + city);
Log.d(TAG, "getAddress: state" + state);
Log.d(TAG, "getAddress: postalCode" + postalCode);
Log.d(TAG, "getAddress: knownName" + knownName);
}
} catch (IOException e) {
e.printStackTrace();
}
return;
}
使用它对我有用
public class LocationAddress {
private static final String TAG = "LocationAddress";
private static String area = null;
public static void getAddressFromLocation(final double latitude, final double longitude,
final Context context, final Handler handler) {
Thread thread = new Thread() {
@Override
public void run() {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
List<Address> addressList = geocoder.getFromLocation(
latitude, longitude, 1);
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append("n");
}
area = address.getSubLocality();
sb.append(address.getLocality()).append("n");
sb.append(address.getPostalCode()).append("n");
sb.append(address.getCountryName());
result = sb.toString();
}
} catch (IOException e) {
Log.e(TAG, "Unable connect to Geocoder", e);
} finally {
Message message = Message.obtain();
message.setTarget(handler);
if (result != null) {
message.what = 1;
Bundle bundle = new Bundle();
/*result = "Latitude: " + latitude + " Longitude: " + longitude +
"nnAddress:n" + result;*/
bundle.putString("address", result);
bundle.putString("area",area);
message.setData(bundle);
} else {
message.what = 1;
Bundle bundle = new Bundle();
/* result = "Latitude: " + latitude + " Longitude: " + longitude +
"n Unable to get address for this lat-long.";*/
bundle.putString("address", result);
bundle.putString("area",area);
message.setData(bundle);
}
message.sendToTarget();
}
}
};
thread.start();
}
}
要在Activity
使用中调用此函数
LocationAddress locationAddress = new LocationAddress();
locationAddress.getAddressFromLocation(latitude,longitude,
getApplicationContext(), new GeocoderHandler());
地理编码器处理程序
private class GeocoderHandler extends Handler {
@Override
public void handleMessage(Message message) {
String locationAddress = null;
String area = null;
switch (message.what) {
case 1:
Bundle bundle = message.getData();
locationAddress = bundle.getString("address");
area = bundle.getString("area");
break;
default:
locationAddress = null;
}
if(locationAddress != null)
locationAddress=locationAddress.replaceAll("[rn]+", " ");
Log.d("===========>>>",area);
Log.d("===========>>>>",locationAddress);
}
}