简体   繁体   中英

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,

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

你可以尝试谷歌储备,地理编码 ,如果你想找到的地方,尝试谷歌的地方

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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