我可以在ActivityMaps.java文件中实现反向地理编码功能吗?
我目前正在Android Studio 1.1.0中开发一个应用程序,我已经实现了Google Maps API,从中我可以检索用户当前位置,但我想将其转换为地址。我可以在MapsActivity.java文件中使用以下代码吗我可以在ActivityMaps.java文件中实现反向地理编码功能吗?,java,android,google-maps,geocoding,reverse-geocoding,Java,Android,Google Maps,Geocoding,Reverse Geocoding,我目前正在Android Studio 1.1.0中开发一个应用程序,我已经实现了Google Maps API,从中我可以检索用户当前位置,但我想将其转换为地址。我可以在MapsActivity.java文件中使用以下代码吗 function getReverseGeocodingData(lat, lng) { var latlng = new google.maps.LatLng(lat, lng); // This is making the Geocode request
function getReverseGeocodingData(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
// This is making the Geocode request
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status !== google.maps.GeocoderStatus.OK) {
alert(status);
}
// This is checking to see if the Geoeode Status is OK before proceeding
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
var address = (results[0].formatted_address);
}
});
}
任何帮助都将不胜感激。Android Studio仍然相对较新您可以使用Android.location.Geocoder轻松完成此操作
// get current locality based on lat lng
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 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();
//基于lat lng获取当前位置
地理编码器;
列出地址;
geocoder=新的geocoder(这个,Locale.getDefault());
地址=地理编码器。getFromLocation(纬度,经度,1);//这里1表示返回的最大位置结果,由它建议的1到5个文档决定
字符串地址=地址。get(0)。getAddressLine(0);//如果存在除“仅”之外的任何其他地址行,请通过getMaxAddressLineIndex()使用最大可用地址行进行检查
字符串city=addresses.get(0.getLocation();
字符串状态=addresses.get(0.getAdminArea();
字符串country=addresses.get(0.getCountryName();
字符串postalCode=addresses.get(0.getPostalCode();
字符串knownName=addresses.get(0.getFeatureName();
为了获得良好的实践,请确保在异步任务中执行此操作