繁体   English   中英

通过提供lat和long来获取地名

[英]get place name by providing lat and long

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

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

仅供参考,你所做的就是反向地理编码。

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

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

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

在这个天气API中,没有必要调用Geocoder ,BTW不公开,所以你应谨慎使用它, 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());

在您的方法中传递经度和纬度,并获得相应的地名。 不要忘记用户权限:

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();
}

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

对于这个东西,Android有一个名为Geocoder的类。 查看getFromLocation方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM