我正在使用Android上的GoogleMap。
到目前为止,我已获得当前位置并在其上显示标记。
我从当前位置获得了LatLong值。
我可以使用以下代码获取城市名称:
Geocoder gcd = new Geocoder(MainActivity.this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation( mLocation.getLatitude(), mLocation.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
Toast.makeText(MainActivity.this,""+addresses.get(0).getLocality(),Toast.LENGTH_LONG).show();
}
但是,无法获得该地区的名称,即 Bodakdev char rasta ,艾哈迈达巴德。
现在,我的问题是:如何从LatLong获取实际文本值(地名)?
答案 0 :(得分:1)
您可以在Google地图中通过Geocoder
对象获取此内容。方法getFromLocation(double, double, int)
完成工作。
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1); // 1 represent max location result to returned, by documents it recommended 1 to 5
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
答案 1 :(得分:1)
您还可以从该地点的邮政编码获得邮政编码
private fun getLatLngByZipcode(zipcode: String): String {
var place = context.getString(R.string.location)
val geocoder = Geocoder(context, Locale.getDefault())
try {
val addresses = geocoder.getFromLocationName(zipcode, 5)
addresses?.forEach{
it?.locality?.apply {
place=this
}
}
}
catch (e: IOException) {
LogUtils.error(TAG,e.message)
}
return place
}
答案 2 :(得分:0)
使用此,
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
String cityName = addresses.get(0).getAddressLine(0);
String stateName = addresses.get(0).getAddressLine(1);
String countryName = addresses.get(0).getAddressLine(2);
String address = addresses.get(0).getAddressLine(0);