get place name by providing lat and long

喜你入骨 提交于 2019-12-21 05:55:08

问题


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.


回答1:


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?




回答2:


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



回答3:


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




回答4:


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



回答5:


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



来源:https://stackoverflow.com/questions/2363608/get-place-name-by-providing-lat-and-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!