Android的 - 如何从坐标获取街道名称? [英] Android - How to get street name from coordinates?

查看:123
本文介绍了Android的 - 如何从坐标获取街道名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有经度和纬度分成两个单独的EditText我希望当我按下一个按钮时,街道名称出现在另一个EditText中。

I have the longitude and latitude into two separate EditText I want that when I press a button the street name appears in another EditText.

我尝试过使用 Public Address getAddressForLocation 方法,但我没有得到它的工作..

I tried with the Public Address getAddressForLocation method, but I have not gotten it to work..

代码

public Address getAddressForLocation(Context context, Location location) throws IOException {

        if (location == null) {
            return null;
        }
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        int maxResults = 1;

        Geocoder gc = new Geocoder(context, Locale.getDefault());
        List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);

        for (int i = 0; i < addresses.getMaxAddressLineIndex(); i++) {
            Log.d("=Adress=",addresses.getAddressLine(i));
        }
    }

如何从坐标获取街名?

更新(解决方案)

UPDATE (SOLUTION)

Geocoder geocoder = new Geocoder(this, Locale.getDefault());

            try {
                List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

                if (addresses != null) {
                    Address returnedAddress = addresses.get(0);
                    StringBuilder strReturnedAddress = new StringBuilder();
                    for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("");
                    }
                    et_lugar.setText(strReturnedAddress.toString());
                }
                else {
                    et_lugar.setText("No Address returned!");
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                et_lugar.setText("Canont get Address!");
            }

谢谢

Thanks

推荐答案

解决方案

SOLUTION

Geocoder geocoder = new Geocoder(this, Locale.getDefault());

                try {
                    List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

                    if (addresses != null) {
                        Address returnedAddress = addresses.get(0);
                        StringBuilder strReturnedAddress = new StringBuilder();
                        for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                            strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("");
                        }
                        et_lugar.setText(strReturnedAddress.toString());
                    }
                    else {
                        et_lugar.setText("No Address returned!");
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    et_lugar.setText("Canont get Address!");
                }

这篇关于Android的 - 如何从坐标获取街道名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆