如何从给定的纬度和经度中查找位置名称?
Posted
技术标签:
【中文标题】如何从给定的纬度和经度中查找位置名称?【英文标题】:How to find location name from given lattitude & longitude? 【发布时间】:2018-01-08 00:45:00 【问题描述】:我正在尝试获取位置名称或地址。我已经通过 Google Fused Location API 成功获取了经纬度。
现在我想通过使用纬度和经度来获取位置地址(例如:城市名称、路号或特定地址)。
为此,我使用了 Google Geocoder,它运行良好。但在某些设备中,位置地址返回空值。
我在网上搜索了这个问题的解决方案,发现一些设备制造商在他们的设备中没有包含这个功能。这就是为什么那些设备无法通过反向地理编码方法找到地址的原因。 Here is the link of that information
那么有没有其他方法可以在不使用 Geoconding 的情况下将地址名称作为字符串查找?
这是我通过地理编码获取地址的代码
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;
【问题讨论】:
【参考方案1】:使用它对我有用
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 +
"\n\nAddress:\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());
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("[\r\n]+", " ");
Log.d("===========>>>",area);
Log.d("===========>>>>",locationAddress);
【讨论】:
你的 GeocoderHandler 类在哪里?它给出了错误 我加了你可以查看 它在某些设备上运行,而在其他设备上崩溃。工作设备 - 支持地理编码的设备,崩溃设备 - 不支持地理编码的设备以上是关于如何从给定的纬度和经度中查找位置名称?的主要内容,如果未能解决你的问题,请参考以下文章
通过给定的经度和纬度查找在数据库中注册的位置 - Laravel