Gilberto Ibarra
Gilberto Ibarra

Reputation: 2927

Highlight whole countries in Google Maps for Android

Referring to:

Highlight whole countries in Google Maps

I'm trying to return the location of selected countrys by the user in the Google Maps Android application.

Something like this but in Android google maps. Is there a possibility to do this in android?

Is it possible to do this offline?

enter image description here

Upvotes: 7

Views: 10619

Answers (2)

Moumen Hamada
Moumen Hamada

Reputation: 51

Basically there are two things you are trying to do.

1- Reverse geocode in order to get the position of the clicked country. I used a repo that I found on GitHub in order to do this. Since it was efficient and works offline. --> https://github.com/AReallyGoodName/OfflineReverseGeocode

2- As soon as you get the clicked country, you need to highlight it. as explained by @xomena

You can also check this repo that I made which basically implements this.

https://github.com/moumen7/Map_picker

Upvotes: 0

xomena
xomena

Reputation: 32178

The question about country or other features borders in Google Maps APIs was asked many times, however, unfortunately Google doesn't expose this data publicly.

To highlight the country you should apply your own data as the Google Maps layer. Nice work around was proposed in the following answer

https://stackoverflow.com/a/40172098/5140781

You can download country borders in GeoJSON format from OSM. After that you can use the Google Maps Android API Utility Library to add GeoJsonLayer in your application.

For my example I downloaded the boundaries of Spain in GeoJSON format and loaded the GeoJSON file using GeoJsonLayer.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        mMap.getUiSettings().setZoomControlsEnabled(true);

        LatLng madrid = new LatLng(40.416775,-3.70379);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(madrid, 3F));

        try {
            GeoJsonLayer layer = new GeoJsonLayer(mMap, R.raw.es_geojson, getApplicationContext());

            GeoJsonPolygonStyle style = layer.getDefaultPolygonStyle();
            style.setFillColor(Color.MAGENTA);
            style.setStrokeColor(Color.MAGENTA);
            style.setStrokeWidth(1F);

            layer.addLayerToMap();

        } catch (IOException ex) {
            Log.e("IOException", ex.getLocalizedMessage());
        } catch (JSONException ex) {
            Log.e("JSONException", ex.getLocalizedMessage());
        }
    }
}

You can download the complete sample project from github, don't forget to change the API key in values/google_maps_api.xml

https://github.com/xomena-so/so41431384

enter image description here

I hope this helps!

Upvotes: 18

Related Questions