简单的示例反向地理编码osmdroid

时间:2014-04-07 23:59:18

标签: map osmdroid nominatim

我正在寻找一个使用osmdroid进行反向地理编码的简单示例。 我有没有使用带有JSON等的nominatimAPI? 我听说使用Geocoder类做同样的事情但似乎太容易了...... 我正在尝试执行请求时,无法识别类RequestBuilder是否正常 对于nominatim?

由于

2 个答案:

答案 0 :(得分:1)

您可以使用OSMBonusPack GeocoderNominatim课程。

答案 1 :(得分:0)

以下是使用OSMBonusPack的示例:

        // declare your map somewhere in the Activity
        map = (MapView) findViewById(R.id.map);
        map.setTileSource(TileSourceFactory.MAPNIK);
        map.setMultiTouchControls(true);

        // create a GeoPoint
        final GeoPoint startPoint = new GeoPoint(36.716999, 3.042076);

        // Retreive Geocoding data (add this code to an event click listener on a button)
        new AsyncTask<Void, Void, Void>(){
            @Override
            protected Void doInBackground(Void... voids) {
                // Reverse Geocoding
                GeocoderNominatim geocoder = new GeocoderNominatim(userAgent);
                String theAddress;
                try {
                    List<Address> addresses = geocoder.getFromLocation(startPoint.getLatitude(), startPoint.getLongitude(), 1);
                    StringBuilder sb = new StringBuilder();
                    if (addresses.size() > 0) {
                        Address address = addresses.get(0);
                        int n = address.getMaxAddressLineIndex();
                        Log.d("Test", "CountryName: " + address.getCountryName());
                        Log.d("Test", "CountryCode: " + address.getCountryCode());
                        Log.d("Test", "PostalCode " + address.getPostalCode());
//                        Log.d("Test", "FeatureName " + address.getFeatureName()); //null
                        Log.d("Test", "City: " + address.getAdminArea());
                        Log.d("Test", "Locality: " + address.getLocality());
                        Log.d("Test", "Premises: " + address.getPremises()); //null
                        Log.d("Test", "SubAdminArea: " + address.getSubAdminArea());
                        Log.d("Test", "SubLocality: " + address.getSubLocality());
//                        Log.d("Test", "SubThoroughfare: " + address.getSubThoroughfare()); //null
//                        Log.d("Test", "getThoroughfare: " + address.getThoroughfare()); //null
                        Log.d("Test", "Locale: " + address.getLocale());
                        for (int i=0; i<=n; i++) {
                            if (i!=0)
                                sb.append(", ");
                            sb.append(address.getAddressLine(i));
                        }
                        theAddress = sb.toString();
                    } else {
                        theAddress = null;
                    }
                } catch (IOException e) {
                    theAddress = null;
                }
                if (theAddress != null) {
                    Log.d("Test", "Address: " + theAddress);
                }

                return null;
            }
        }.execute();

可以在Wiki页面上找到更多教程: https://github.com/MKergall/osmbonuspack/wiki 希望这会有所帮助。