android - 无法从 Android 中的反向地理编码打印地址

标签 android google-maps android-emulator reverse-geocoding

我引用了Stack overflow的很多问题,实现了上面的过程。但我无法获得地址。请让我知道如果我错过了什么..? myLoc = (TextView) findViewById(R.id.id1);

Geocoder geocoder = new Geocoder(getBaseContext(),Locale.getDefault());
try {
    address = geocoder.getFromLocation(latitude, longitude, 1);
                if (address.size() > 0) {
        for (int i = 0; i < address.get(0)
                .getMaxAddressLineIndex(); i++) {
            display = "";
            display += address.get(0).getAddressLine(i)
                    + "\n";
        }
        }

} catch (Exception e2) {
    // TODO: handle exception
}
myLoc.setText("Current Location:"+display);
System.out.println(display);

最佳答案

您可以使用反向地理编码和 Google api 从纬度和经度获取地址。

反向地理编码:

 double currentLatitude;
    double currentLongitude;

void getAddress(){
        try{
            Geocoder gcd = new Geocoder(this, Locale.getDefault());
            List<Address> addresses = 
                gcd.getFromLocation(currentLatitude, currentLongitude,100);
            if (addresses.size() > 0) {
                StringBuilder result = new StringBuilder();
                for(int i = 0; i < addresses.size(); i++){
                    Address address =  addresses.get(i);
                    int maxIndex = address.getMaxAddressLineIndex();
                    for (int x = 0; x <= maxIndex; x++ ){
                        result.append(address.getAddressLine(x));
                        result.append(",");
                    }               
                    result.append(address.getLocality());
                    result.append(",");
                    result.append(address.getPostalCode());
                    result.append("\n\n");
                }
                addressText.setText(result.toString());
            }
        }
        catch(IOException ex){
            addressText.setText(ex.getMessage().toString());
        }
    }

Google API:请参阅此 api,它从纬度和经度返回地址

http://maps.googleapis.com/maps/api/geocode/json?latlng=17.734884,83.299507&sensor=true

To know more read this

关于android - 无法从 Android 中的反向地理编码打印地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12056694/

相关文章:

android - 如何解决android MapView 中的IllegalStateException。 (指定的 child 已经有一个父必须调用 removeView())?

javascript - 将位置坐标作为变量传递给谷歌地图

javascript - google maps api 附加搜索框

google-maps - 谷歌地图破坏 KML

Android Facebook 集成在 Android 设备上出现无效 key 哈希错误,但在模拟器上运行良好

android - 将应用程序快速部署到 Android 模拟器总是失败

android - Xamarin Forms 编辑器文本未在 android 上正确显示

android - htaccess 针对代理的保护

android - 如何从 Uri 调整图像大小?

android - 如何将 Cookie session id 放入 volley request 中?