2

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

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

4

5 回答 5

3

不需要Geocoder在这个天气 API 中调用 , 顺便说一句,它是不公开的,所以你应该谨慎使用它citystate并且country只是信息字段,所以你可以像这样使用它(虚拟常量):

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());
于 2010-03-02T15:22:11.750 回答
3

仅供参考,您正在做的事情称为反向地理编码。

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

谷歌还有一个反向地理编码网络服务

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

于 2010-03-02T14:11:34.887 回答
2

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

于 2010-03-02T14:12:53.380 回答
2

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

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();
}
于 2012-05-09T13:08:39.530 回答
1

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

于 2010-03-02T14:30:17.213 回答