在MainActivity.java文件中加上此方法,其中lnt为经度,lat为纬度
1 public String getAddress(double lnt, double lat) { 2 3 Geocoder geocoder = new Geocoder(MainActivity.this); 4 boolean falg = geocoder.isPresent(); 5 StringBuilder stringBuilder = new StringBuilder(); 6 try { 7 //根据经纬度获取地理位置信息---这里会获取最近的几组地址信息,具体几组由最后一个参数决定 8 List<Address> addresses = geocoder.getFromLocation(lat, lnt, 1); 9 if (addresses.size() > 0) { 10 Address address = addresses.get(0); 11 for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { 12 if (i == 0) { 13 stringBuilder.append(address.getAddressLine(i)).append(""); 14 } 15 if (i == 1) { 16 stringBuilder.append(address.getAddressLine(i)); 17 break; 18 } 19 } 20 stringBuilder.append(address.getFeatureName()).append("");//周边地址 21 mLocal.setText("地理位置:"+stringBuilder); 22 } 23 } catch (Exception e) { 24 e.printStackTrace(); 25 } 26 return stringBuilder.toString(); 27 }
然后再调用此方法即可
效果图如下: