2

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

谢谢

4

2 回答 2

1

您可以使用OSMBonusPack GeocoderNominatim类。

于 2014-04-08T07:10:54.913 回答
1

这是一个使用 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 希望这会有所帮助。

于 2019-03-11T19:23:03.813 回答