在我的当前的安卓应用中,我想根据输入的城市名、街道名或邮政编码获取地理坐标。我该如何实现呢?
最好的问候, Rony
在我的当前的安卓应用中,我想根据输入的城市名、街道名或邮政编码获取地理坐标。我该如何实现呢?
最好的问候, Rony
请查看方法Geocoder.getFromLocationName(cityName, maxResults)
。
使用方法如下:
List<Address> addressList = geoCoder.getFromLocationName(cityName, 1);
Address address = addressList.get(0);
if(address.hasLatitude() && address.hasLongitude()){
double selectedLat = address.getLatitude();
double selectedLng = address.getLongitude();
}
您好,请尝试以下代码以从给定地址获取地理编码点。
List<Address> foundGeocode = null;
/* find the addresses by using getFromLocationName() method with the given address*/
foundGeocode = new Geocoder(this).getFromLocationName("address here", 1);
foundGeocode.get(0).getLatitude(); //getting latitude
foundGeocode.get(0).getLongitude();//getting longitude
试试这个。
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(latitude , longitude, 1);
String strCompleteAddress= "";
//if (addresses.size() > 0)
//{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
strCompleteAddress+= addresses.get(0).getAddressLine(i) + "\n";
// }
Log.i("MyLocTAG => ", strCompleteAddress);
Toast.makeText(getBaseContext(), strCompleteAddress, Toast.LENGTH_LONG).show();
}
catch (IOException e) {
Log.i("MyLocTAG => ", "this is the exception part");
e.printStackTrace();
}
Geocoder
可能比下载地点和坐标数据库更好的解决方案。 - Kev