Aswan
Aswan

Reputation: 5135

get place name by providing lat and long

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.

Upvotes: 2

Views: 5225

Answers (5)

Ashok Domadiya
Ashok Domadiya

Reputation: 1122

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

Upvotes: 2

Diego Torres Milano
Diego Torres Milano

Reputation: 69358

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

Upvotes: 3

Luca
Luca

Reputation: 1118

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

Upvotes: 1

Donal Rafferty
Donal Rafferty

Reputation: 19826

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

Upvotes: 2

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

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?

Upvotes: 3

Related Questions