Getting a Polygon from a Location

Last updated:

Some locations in MapsIndoors can have additional polygon information. These polygons can be used to render a room or area in a special way or make geofences, calculating whether another point or location is contained within the polygon. If a MPLocation has polygons, these can be retrieved using:

Geometry geometry = location.getGeometry();
switch( geometry.getIType() ) {
case Geometry.TYPE_POINT: {
Point point = (Point) geometry;
break;
}
case Geometry.TYPE_POLYGON: {
PolygonGeometry polygon = (PolygonGeometry) geometry;

// Using GMS helper classes
// Get all the paths in the polygon
final List<List<LatLng>> paths = polygon.getGMSPath();

final int pathCount = paths.size();

// Outer ring (first)
List<LatLng> path = paths.get( 0 );
for( final LatLng coordinate : path ) {
double lat = coordinate.latitude;
double lng = coordinate.longitude;
}

// Optional: Inner rings (holes)
for( int i = 1; i < pathCount; i++ ) {
List<LatLng> hole = paths.get( i );
for( final LatLng coordinate : hole ) {
double lat = coordinate.latitude;
double lng = coordinate.longitude;
}
}
break;
}
}

As demonstrated above, a polygon's outer ring/path as well as holes are arranged as [longitude, latitude] pairs. As not all locations has polygons, the polygon array may be empty. On the contrary, some locations, like entire building floors, might have more than polygon.

  1. Developing on the new Arm-based Apple Silicon (M1) Macs requires building and running on a physical iOS device or using an iOS simulator running iOS 13.7, e.g. iPhone 11. This is a temporary limitation in Google Maps SDK for iOS, and as such also a limitation in MapsIndoors, due to the dependency to Google Maps.
  2. Note: Due to a bug in CocoaPods it is necessary to include the post_install hook in your Podfile described in the PodFile post_install wiki.

Some locations in MapsIndoors can have additional polygon information. These polygons can be used to render a room or area in a special way or make geofences, calculating whether another point or location is contained within the polygon. If a MPLocation has polygons, these can be retrieved using the MPGeometryHelper class.

let polygons = MPGeometryHelper.polygonsForLocation(location : MPLocation)
let polygon = polygons.first
let path = polygon.path
let holes = polygon.holes
for coordinate in path {
let lat = coordinate[1]
let lng = coordinate[0]
}

As demonstrated above, a polygon's outer ring/path as well as holes are arranged as [longitude, latitude] pairs. As not all locations has polygons, the polygon array may be empty. On the contrary, some locations, like entire building floors, might have more than polygon.