1

I am using Google Maps Android Utility SDK, using GeoJSON features.

I would like to, based on the user Location (which I already have) determine the features that contain this point.

Does Google Maps provide such utility or should I implement by myself?

1

You should be able to do quick calculations using the bounding box with:

Location location = ...;
GeoJsonFeature feature = ...
LatLng point = new LatLng(location.getLatitude(), location.getLongitude());

if (feature.getBoundingBox().contains(point)) {
    // Polygon contains the point
}

If you want to use the actual polygon geometry, that's a little more complicated. PolyUtil.containsLocation(LatLng point, List<LatLng> polygon, boolean geodesic) returns a true if the polygon contains the point, and false if it does not.

Given a GeoJsonFeature, I believe (without having tested it myself) you can extract the points from that feature and then pass those points into PolyUtil.containsLocation().

So, for testing actual polygon geometry:

Location location = ...;
GeoJsonFeature feature = ...
LatLng point = new LatLng(location.getLatitude(), location.getLongitude());

if (feature.getGeometry() instanceof GeoJsonPolygon) {
    GeoJsonPolygon polygon = (GeoJsonPolygon) feature.getGeometry();
    if (PolyUtil.containsLocation(point, polygon.getOuterBoundaryCoordinates(), false) {
        // Polygon contains the point            
    }
}
| improve this answer | |
  • I guess this could potentially work, if the Geometry isn't a MultiPolygon. But what if it is? I understand that doing that check by iterating in all the Polygons of the MultiPolygon would do the trick. – xarlymg89 Dec 30 '19 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.