How to get address from latitude and longitude android?

淺唱寂寞╮ 提交于 2019-12-12 23:52:09

问题


I have an application that will get the latitude and the longitude of the users location. What I need to know is how do I take the latitude and the longitude of the location and convert it into just the address of the user?

Thanks for any help.


回答1:


You can use GeoCoding. Follow the instructions on this link where it says Geocoding and Reverse Geocoding

You can find code there that you can try it out on your own app (copy/paste)

I hope it helps :)




回答2:


Try using Geocoder




回答3:


     try {
         Geocoder geocoder;
         List<Address> addresses;
         geocoder = new Geocoder(MainActivity.this);
         Toast.makeText(this, "before if",
                 Toast.LENGTH_LONG).show();

         if (latitude != 0 || longitude != 0) {
             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);
          Log.d("TAG", "address = "+address+", city ="+city+", country = "+country );
         Toast.makeText(this, "address = "+address+", city ="+city+", country = "+country, Toast.LENGTH_LONG).show();

         } 
         else {
             Toast.makeText(this, "latitude and longitude are null",
                     Toast.LENGTH_LONG).show();

         }
     } catch (Exception e) {
         e.printStackTrace();

     }


来源:https://stackoverflow.com/questions/7894804/how-to-get-address-from-latitude-and-longitude-android

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