Bryanyan
Bryanyan

Reputation: 677

Get address from latitude and longitude

I am trying to get location address from Latitude and longitude in android. My code is as follow.

public String getAddress(double latitude, double longitude) {
        Geocoder myLocation = new Geocoder(this, Locale.getDefault());
        List<Address> myList = null;
        try {
            myList = myLocation.getFromLocation(latitude,longitude,1);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String address = myList.get(0).getAddressLine(0);
        return address;
    }

But somehow I get null return at myList = myLocation.getFromLocation(latitude,longitude,1); What could be wrong?

Best,

Upvotes: 0

Views: 1036

Answers (4)

Sunil Kumar
Sunil Kumar

Reputation: 7092

You can use via google api like insert your longitude and latitude with parameter and send request to the server and server giving the response with address.

public static String getAddressUrl(Context context) { String responseText=null;

  /*String latitude="38.89";
  String longitude ="-77.03";*/
  Log.v(TAG , "Latitude is: " + latitude + "Longitude is:"+ longitude);
  StringBuilder sbuilder=new StringBuilder();
      String googleurl="http://maps.google.com/maps/geo?"
  sbuilder.append(googleurl);

  sbuilder.append("q="+latitude+","+longitude);
  sbuilder.append("&amp;output=responseText&amp;sensor=true");
  String url=sbuilder.toString();

 Log.v(TAG, "url is: "+url); 
  try { 
      DefaultHttpClient httpclient=new DefaultHttpClient(); 
      HttpPost httppost=new HttpPost(url); 
      HttpResponse httpresponse=httpclient.execute(httppost); 
      HttpEntity httpentity=httpresponse.getEntity(); 
      InputStream is=httpentity.getContent();
      BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb=new StringBuilder();
      String line=null;
    while((line=reader.readLine())!=null)
     { 
      sb.append(line+"\n");

     } 
    is.close(); 
    responseText=sb.toString();
  } 
  catch(ClientProtocolException e) 
  { 
      e.printStackTrace(); 
  } 
  catch (IOException e) 
  { 
      e.printStackTrace();
  }
  return responseText;
  }

Upvotes: 0

Tai Dao
Tai Dao

Reputation: 3437

You can try google reserve-geocoding if you want to find places, try google place

Upvotes: 1

Vipul
Vipul

Reputation: 28093

It is common.You will get Null if server does not return any value or if it is overloaded.

I faced similar issue and got the solution on following post.

http://girishbhutiya.blogspot.in/2010/04/reverse-geocoding-in-android-2-2-froyo-api-level-greater-than-8.html

Upvotes: 2

CRUSADER
CRUSADER

Reputation: 5472

Try, the following code.

try {
                    // Get the location manager
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                        Criteria criteria = new Criteria();
                        bestProvider = locationManager.getBestProvider(criteria, false);
                        Location location = locationManager
                                .getLastKnownLocation(bestProvider);
                        double lat = location.getLatitude();
                        double lon = location.getLongitude();
                        Geocoder gc = new Geocoder(this);
                        List<Address> lstAdd = gc.getFromLocation(lat, lon, 1);
                        Address ad = lstAdd.get(0);
                        String str = ad.getAddressLine(0);
                    Toast tst = Toast.makeText(this, "Your current location is " + str,
                            Toast.LENGTH_LONG);
                    tst.show();
    } catch (Exception e) {
                    Toast tst = Toast.makeText(this,"Please check your gps settings",
                            Toast.LENGTH_LONG);
                    tst.show();
    }

But, ofcourse try on real device.

Upvotes: 0

Related Questions