How to get address of a location from latitude longitude values using geocoder in android?

放肆的年华 提交于 2019-12-29 08:20:39

问题


i am tried up with some common set of codes, to get convert the lat long values into address. But i am unable to get that in all the time.. which would be the best option for this. I need this service should be constant. because i need to deliver this to a client.

suggest me a good solution? Should i have to buy a service from google or yahoo? Those peoples are providing separate service for this. My main question is should this "geocoder" will be ok or not??

My code is

void getAddress(){
try{
    Geocoder gcd = new Geocoder(this, Locale.getDefault());

    List<Address> addresses = gcd.getFromLocation(latitude, longitude,100);

    Log.d("latlong value",latitude +"---"+longitude);
    if (addresses.size() > 0) 
    {
        StringBuilder result = new StringBuilder();
        result.append(addresses.get(0).getCountryName());
        country=result.toString();
    }
      }
catch(IOException ex){

}
}

回答1:


should this "geocoder" will be ok or not?

Yes it is absolutely fine to use Geocode class.

Try out this very simple code,

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);

String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);


来源:https://stackoverflow.com/questions/21930665/how-to-get-address-of-a-location-from-latitude-longitude-values-using-geocoder-i

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