Behnam Azimi
Behnam Azimi

Reputation: 2488

Specify an area on Google Map as support area - Google Maps Android Api

I'm using Google Map API in my Android project, everything is OK but I want to have a support area on a map to allow users to pin own addresses just on that area.

In JavaScript there is a kmlLayer and a listener for it to find out the kmllayer click. On this way I get the latitude and longitude of the clicked point.

But in android there is just a setOnFeatureClickListener listener for kmlLayer, (Google Map Android kmlLayer Documentation) I want to show my area with a kml polygon and just make it clickable or get a latitude and longitude of clicked point of the layer.

Except getting the latitude and longitude of the touched point that I explained above, I can get the map center coordinates. So if I could check that the map center is in the kml polygon, my issue will solve!

To solve my problem, I tried many ways and many technics and asked many questions about. In one of my questions I tried to trigger a touch listener on a map layer.

I also apologize for my poor English, I think my explanation is clear If you don't think so, please comment, I will explain it more!

Upvotes: 0

Views: 498

Answers (1)

Amir_P
Amir_P

Reputation: 9019

first you should create an object of LatLngBounds.Builder and then iterate over KML's KmlContainer like this

public void accessContainers(Iterable<KmlContainer> containers) {
    for(KmlContainer c : containers) {
        if(c.hasPlacemarks()) {
            for(KmlPlacemark p : c.getPlacemarks()) {
                Geometry g = p.getGeometry();
                Object object = g.getGeometryObject();

                if(object instanceof LatLng) {
                    LatLng latlng = (LatLng)object;
                    latLngBoundsBuilder.include(latlng);
                }

                if(object instanceof List<?>) {
                    List<LatLng> list = (List<LatLng>)object;
                    for(LatLng latlng : list) {
                        latLngBoundsBuilder.include(latlng);
                    }
                }

                Log.d(TAG, g.getGeometryType() + ":" + object.toString());
            }
        }
        if(c.hasContainers()) {
            accessContainers(c.getContainers());
        }
    }
}

now using latLngBoundsBuilder.build() get an object of LatLngBounds. Then implement setOnCameraIdleListener and inside onCameraIdle check if google map center is inside that bound

latLngBounds.contains(map.getCameraPosition().target)

Upvotes: 1

Related Questions