How to get addresses if we give latitude and longitude using location API in Android

泪湿孤枕 提交于 2019-12-25 00:35:03

问题


I have an application where I try to get address of a location based on the latitude,longitude coordinates.When I try to print all the address of particular coordinates , I am getting only single address. IS there any way to get the list of all addresses for the coordinates supplied . My code snippet is as below:

Where locationLatitude,locationLongitude are of type String which are converted to doubles

****List addresses = geoCoder.getFromLocation(Double .parseDouble(locationLatitude), Double.parseDouble(locationLongitude), 1); StringBuffer addressAsString = new StringBuffer(""); if(!addresses.isEmpty()){ for (int i = 0; i < addresses.get(0).getMaxAddressLineIndex(); i++) addressAsString.append(addresses.get(0).getAddressLine(i) + "\n"); return addressAsString.toString() ;****

I will be waiting for any reply. Thanks in Advance.


回答1:


You must read the api carefully:

Public Methods List getFromLocation(double latitude, double longitude, int maxResults)

Returns an array of Addresses that are known to describe the area immediately surrounding the given latitude and longitude.

If you change maxResults parameter from 1 to 10, then 10 results will returned if there has.

List addresses = geoCoder.getFromLocation(Double .parseDouble(locationLatitude), Double.parseDouble(locationLongitude), 10); 



回答2:


Loop through all your addresses:

List addresses = geoCoder.getFromLocation(Double .parseDouble(locationLatitude), Double.parseDouble(locationLongitude), 10); 

for (Address addr:List addresses) {

  for (int i = 0; i < addr.getMaxAddressLineIndex(); i++) {
  // your previous code here
  }
}


来源:https://stackoverflow.com/questions/3649401/how-to-get-addresses-if-we-give-latitude-and-longitude-using-location-api-in-and

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