通过提供纬度和经度获取地名

发布于 2024-08-23 15:44:05 字数 202 浏览 3 评论 0原文

在我的应用程序中,我想获取我正在使用的天气预报 http://www.google .com/ig/api?weather="" 通过提供城市来实现这一点。但对我来说,只有纬度和经度可用,

如何通过纬度和经度获取地名。

In my application i want to get the weather report I'm using http://www.google.com/ig/api?weather="" for that by providing a city. But for me only lat and longitude is available

How can I get the place name by lat and longitudes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心头的小情儿 2024-08-30 15:44:05

仅供参考,您所做的称为反向地理编码。

大多数 Geonames Web 服务都以 JSON 形式提供,包括 findNearby。

Google 还提供反向地理编码网络服务

这不是一件小事 - lat/lng 数据库几乎肯定会以 XML 或 JSON 形式呈现,因此可能值得在这方面投入一些时间和精力。 Android 必须有 xml/json 包装器吗?

FYI, what you're doing is called reverse geocoding.

Most of the Geonames webservices are available in JSON, including findNearby.

Google also has a reverse geocoding webservice

What you're asking is not a small thing - a lat/lng database is almost certainly going to be presented in either XML or JSON, so it might be worth investing a little time and effort in this. Android must have xml/json wrappers?

情未る 2024-08-30 15:44:05

在这个天气 API 中,无需调用 Geocoder,顺便说一句,该 API 不是公开的,因此您应该谨慎使用它,citystatecountry 只是信息字段,因此您可以像这样使用它(虚拟常量):

private static final String URL = "http://www.google.com/ig/api?weather=%s,%s,%s,%d,%d";
//...

final String url = String.format(URL, "city", "state", "country",
   p.getLatitudeE6(), p.getLongitudeE6());

There's no need to invoke the Geocoder, in this weather API, which BTW is not public so you should use it with caution, city, state and country are only informational fields, so you can use it like this (dummy constants):

private static final String URL = "http://www.google.com/ig/api?weather=%s,%s,%s,%d,%d";
//...

final String url = String.format(URL, "city", "state", "country",
   p.getLatitudeE6(), p.getLongitudeE6());
远昼 2024-08-30 15:44:05

您可以使用 Location API 获取地址对象 并从那里获取位置

You can use the Location API to get a Address Object and get the locality from there

我的黑色迷你裙 2024-08-30 15:44:05

在你的方法中传入经纬度,得到对应的地名。不要忘记用户权限:

public static String getUserLocation(String lat, String lon) {
   String userlocation = null;
   String readUserFeed = readUserLocationFeed(lat.trim() + "," + lon.trim());
   try {
      JSONObject Strjson = new JSONObject(readUserFeed);
      JSONArray jsonArray = new JSONArray(Strjson.getString("results"));
      userlocation = jsonArray.getJSONObject(1)
            .getString("formatted_address").toString();
   } catch (Exception e) {
      e.printStackTrace();
   }
   Log.i("User Location ", userlocation);
   return userlocation;
}

public static String readUserLocationFeed(String address) {
   StringBuilder builder = new StringBuilder();
   HttpClient client = new DefaultHttpClient();
   HttpGet httpGet = new HttpGet(
         "http://maps.google.com/maps/api/geocode/json?latlng=" + address
               + "&sensor=false");
   try {
      HttpResponse response = client.execute(httpGet);
      StatusLine statusLine = response.getStatusLine();
      int statusCode = statusLine.getStatusCode();
      if (statusCode == 200) {
         HttpEntity entity = response.getEntity();
         InputStream content = entity.getContent();
         BufferedReader reader = new BufferedReader(new InputStreamReader(
               content));
         String line;
         while ((line = reader.readLine()) != null) {
            builder.append(line);
         }
      } else {
         Log.e(ReverseGeocode.class.toString(), "Failed to download file");
      }
   } catch (ClientProtocolException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   }
   return builder.toString();
}

Pass the latitude and longitude in your method and get the corresponding place name. Don't forget the user permissions:

public static String getUserLocation(String lat, String lon) {
   String userlocation = null;
   String readUserFeed = readUserLocationFeed(lat.trim() + "," + lon.trim());
   try {
      JSONObject Strjson = new JSONObject(readUserFeed);
      JSONArray jsonArray = new JSONArray(Strjson.getString("results"));
      userlocation = jsonArray.getJSONObject(1)
            .getString("formatted_address").toString();
   } catch (Exception e) {
      e.printStackTrace();
   }
   Log.i("User Location ", userlocation);
   return userlocation;
}

public static String readUserLocationFeed(String address) {
   StringBuilder builder = new StringBuilder();
   HttpClient client = new DefaultHttpClient();
   HttpGet httpGet = new HttpGet(
         "http://maps.google.com/maps/api/geocode/json?latlng=" + address
               + "&sensor=false");
   try {
      HttpResponse response = client.execute(httpGet);
      StatusLine statusLine = response.getStatusLine();
      int statusCode = statusLine.getStatusCode();
      if (statusCode == 200) {
         HttpEntity entity = response.getEntity();
         InputStream content = entity.getContent();
         BufferedReader reader = new BufferedReader(new InputStreamReader(
               content));
         String line;
         while ((line = reader.readLine()) != null) {
            builder.append(line);
         }
      } else {
         Log.e(ReverseGeocode.class.toString(), "Failed to download file");
      }
   } catch (ClientProtocolException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   }
   return builder.toString();
}
守望孤独 2024-08-30 15:44:05

Android 有一个名为 Geocoder 的类来处理这些东西。查看 getFromLocation 方法。

Android has a class called Geocoder for this stuff. Look at the getFromLocation method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文