查看: 1298|回复: 1

如何在Android中使用OpenStreetMap的经纬度获得地址

[复制链接]
已绑定手机
发表于 2022-9-25 19:40:12 | 显示全部楼层 |阅读模式
如何在Android中使用OpenStreetMap的经纬度获得地址
发表于 2022-9-25 19:51:08 | 显示全部楼层
1. 试试这个代码获取地址。Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);

为openstreammap final String requestString = " CodeGo.net  +
   Double.toString(lat) + "&lon=" + Double.toString(lon) + "&zoom=18&addressdetails=1";  
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(requestString));
try {
@SuppressWarnings("unused")
Request request = builder.sendRequest(null, new RequestCallback() {
  @Override
  public void onResponseReceived(Request request, Response response) {
   if (response.getStatusCode() == 200) {
    String city = "";
    try {
     JSONValue json = JSONParser.parseStrict(response);
     JSONObject address = json.isObject().get("address").isObject();
     final String quotes = "^\"|\"$";
     if (address.get("city") != null) {
      city = address.get("city").toString().replaceAll(quotes, "");
     } else if (address.get("village") != null) {
      city = address.get("village").toString().replaceAll(quotes, "");
     }
    } catch (Exception e) {
    }   
   }
  }
});
} catch (Exception e) {
}

2. 这里是我的解决方案。我认为它为你的作品也。 public String ConvertPointToLocation(GeoPoint point) {
  String address = "";
  Geocoder geoCoder = new Geocoder( getBaseContext(), Locale.getDefault());
  try {
   List<Address> addresses = geoCoder.getFromLocation(
    point.getLatitudeE6() / 1E6,
    point.getLongitudeE6() / 1E6, 1);
   if (addresses.size() > 0) {
    for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
     address += addresses.get(0).getAddressLine(index) + " ";
   }

   Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
  }
  catch (IOException e) {   
   e.printStackTrace();
  }
  return address;
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 加入联盟

本版积分规则

快速回复 返回顶部 返回列表