代码之家  ›  专栏  ›  技术社区  ›  Chrispix

Android:反向地理编码-getFromLocation

  •  48
  • Chrispix  · 技术社区  · 16 年前

    Geocoder myLocation = Geocoder(Locale.getDefault());
        List myList = myLocation.getFromLocation(latPoint,lngPoint,1);
    

    任何援助都会有帮助。非常感谢。


    谢谢,我首先尝试了上下文,LocaleOne,但失败了,并且正在查看其他一些构造函数(我看到一个只提到了locale)。不管

    它不起作用,因为我仍在了解:类型savemaplocation的方法Geocoder(Context,Locale)未定义

    我有:导入android.location.Geocoder;

    8 回复  |  直到 12 年前
        1
  •  71
  •   Wilfred Knievel    16 年前

    下面的代码片段是为我做的(lat和lng是这个位上面声明的双倍):

    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
    
        2
  •  50
  •   EricLarch    14 年前

    Geocoder调用过程,可以位于帮助器类中

    public static void getAddressFromLocation(
            final Location location, final Context context, final Handler handler) {
        Thread thread = new Thread() {
            @Override public void run() {
                Geocoder geocoder = new Geocoder(context, Locale.getDefault());   
                String result = null;
                try {
                    List<Address> list = geocoder.getFromLocation(
                            location.getLatitude(), location.getLongitude(), 1);
                    if (list != null && list.size() > 0) {
                        Address address = list.get(0);
                        // sending back first address line and locality
                        result = address.getAddressLine(0) + ", " + address.getLocality();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Impossible to connect to Geocoder", e);
                } finally {
                    Message msg = Message.obtain();
                    msg.setTarget(handler);
                    if (result != null) {
                        msg.what = 1;
                        Bundle bundle = new Bundle();
                        bundle.putString("address", result);
                        msg.setData(bundle);
                    } else 
                        msg.what = 0;
                    msg.sendToTarget();
                }
            }
        };
        thread.start();
    }
    

    以下是在您的UI活动中对此地理编码器过程的调用:

    getAddressFromLocation(mLastKownLocation, this, new GeocoderHandler());
    

    private class GeocoderHandler extends Handler {
        @Override
        public void handleMessage(Message message) {
            String result;
            switch (message.what) {
            case 1:
                Bundle bundle = message.getData();
                result = bundle.getString("address");
                break;
            default:
                result = null;
            }
            // replace by what you need to do
            myLabel.setText(result);
        }   
    }
    

    不要忘记在您的应用程序中添加以下权限 Manifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
    
        3
  •  37
  •   Reto Meier    16 年前

    1) 你错过了比赛 new 调用构造函数之前的关键字。

    2) 传递给Geocoder构造函数的参数不正确。你要经过一个 Locale Context .

    有两个 Geocoder 构造函数,这两个构造函数都需要 ,还有一个还拿着 场所 :

    Geocoder(Context context, Locale locale)
    Geocoder(Context context)
    

    修改代码以在有效上下文中传递并包含 刚出现的 你应该可以走了。

    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
    List<Address> myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
    

    如果你仍然有问题,这可能是一个许可问题。地理编码隐式地使用Internet来执行查找,因此您的应用程序将需要 INTERNET 在清单中使用权限标记。

    在中添加以下使用权限节点 manifest

    <uses-permission android:name="android.permission.INTERNET" />
    
        4
  •  8
  •   Ingo    9 年前

    原因是不存在后端服务:

        5
  •  5
  •   Manoj Sharma    8 年前

    首先使用Location and LocationManager类获取纬度和经度。现在,请尝试下面的代码以获取城市、地址信息

    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    Geocoder gc = new Geocoder(this, Locale.getDefault());
    try {
        List<Address> addresses = gc.getFromLocation(lat, lng, 1);
        StringBuilder sb = new StringBuilder();
        if (addresses.size() > 0) {
            Address address = addresses.get(0);
            for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                sb.append(address.getAddressLine(i)).append("\n");
                sb.append(address.getLocality()).append("\n");
                sb.append(address.getPostalCode()).append("\n");
                sb.append(address.getCountryName());
        }
    

    城市信息现在在sb中。现在将sb转换为字符串(使用sb.toString()。

        6
  •  2
  •   Adil Soomro    13 年前

    在我离开地图之前,我打电话 SaveLocation(myMapView,myMapController); 这就是我的地理编码信息。

    但是自从 getFromLocation IOException

    try
    {
        SaveLocation(myMapView,myMapController);
    }
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    然后,我必须改变SaveLocation,说它抛出了IOExceptions:

     public void SaveLocation(MapView mv, MapController mc) throws IOException{
        //I do this : 
        Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
        List myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
    //...
        }
    

    它每次都会崩溃。

        7
  •  0
  •   user186939 user186939    15 年前

    我听说Geocoder是有问题的。我最近编写了一个示例应用程序,它使用Google的http地理编码服务从lat/long查找位置。请随意在这里查看

    http://www.smnirven.com/?p=39

        8
  •  0
  •   Yazan    8 年前

    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);