Search code examples
androidosmdroid

How to zoom to a specific country with OSMDroid?


Assume the following situation:
The User chooses a country from a list. After clicking on a country, the map should zoom to the selected country.

How can I achieve the zoomToCountry in OSMDroid if I only know the country name from the list?

In the PHP API "GoogleMaps v3" there is a solution like

function findAddress(address) {
        var geocoder = null;
        geocoder = new google.maps.Geocoder();

        if (!address) 
        var address=document.getElementById("countryselect").value;
        if ((address != '') && geocoder) {
          geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
               if (results && results[0] && results[0].geometry && results[0].geometry.viewport) 
                    map.fitBounds(results[0].geometry.viewport);
              } else {
                alert("No results found");
              }
            } else {
              alert("Geocode was not successful for the following reason: " + status);
            }
          });
        }
    }

Is there any similar in OSMDroid or the OSMBonuspack?


Solution

  • The PHP solution is a call to geocoding service with the country name, to get country bounding box.

    You can do something similar using Nominatim service. So OSMBonusPack GeocoderNominatim should be usable.

    Roughly that (without error handling, AsyncTask and so on):

    GeocoderNominatim coder = new GeocoderNominatim(ctx);
    List<Address> results = coder.getFromLocationName(countryName, 1);
    Address address = results.get(0);
    Bundle extras = address.getExtras();
    BoundingBoxE6 bb = extras.getParcelable("boundingbox");
    

    Then:

    mapView.zoomToBoundingBox(bb);
    

    As simple as PHP/GoogleMaps solution, isn't it?