A geofence is a closed polygon drawn over a geographical area. A geofence may be used to define either an area of inclusion, or an area of exclusion - the behavior depends on your implementation. A geofence is constructed from one or more latitude/longitude coordinate pairs where each coordinate forms a corner of the geofence (or in the case of a CircleFence, the point defines the center of the geofence). The number of coordinates required will depend on the type of geofence.
An example geofence is shown below.
GeoBase provides four types of geofence. You can create your own geofence type by implementing the IGeoFence interface. Each type of geofence can enclose a different shaped area. The four types of geofence are:
To exclude the area from a route, set the Route object's RouteStrategy object's KeepOut property. An example of this is shown below. Note that the KeepOut property can only exclude a single geofence, or a single GeoFenceCollection.
/* define a circular geofence, 50 mile radius */ CircleFence myFence = new CircleFence(); myFence.Center = new LatLon(34.1018, -118.2973); myFence.Radius = UnitsToRadians(50, DistanceUnit.MILES); /* create a Route, excluding the geofence */ Route myRoute.Strategy.KeepOut = myFence;
Both GeoFenceCollection and the GeoFence objects implement the IMapRenderer interface, so to draw them on a map it is a simple case of adding an object of either type to the map's RendererList object.
Any object that implements the IMapRenderer interface can be drawn on a map. You can implement this interface yourself on custom objects. See the Rendering Concept topic for more details.
/* create new RenderList, add it to the map */ RendererList rendererList = new RendererList(); myMap.Renderer = rendererList; /* draw geofence on map */ rendererList.Add(myGeoFence); /* you can add as many other objects you want to draw on the map: rendererList.Add(myOtherGeoFence); rendererList.Add(myBalloonPushPin); ...*/
A reachable area is a special type of geofence or geofence collection defined by the region that can be reached using a route that is no longer than a maximum distance or time, starting from a specified starting location. The area you can reach given a time or distance limit depends on your routing strategy. For example, you can get farther in a limited time if you are using the RoutingStrategyFastest strategy than if you are using the RoutingStrategyShortest, and both those strategies will get you farther than RoutingStrategyForPedestrian.
To calculate reachable areas, you can create a ReachableArea object by WithinDistance or WithinTimeSpan method. Each ReachableArea object represents a routing strategy, a starting point, and a maximum time or distance that a route from the starting point can take.
Calling the ComputeConvexPolygon returns a polygon fence defined by connecting the farthest points that can be reached from the starting point given the routing strategy and time or distance limit. Calling the ComputeConcavePolygon method returns a concave polygon fence, showing the smallest polygon that contains all locations that can be reached. Calling the ComputeStreetLinks method returns a geofence collection containing a street link fence for every street link that can be reached.
/* create new ReachableArea */ ReachableArea walkableInAnHour = ReachableArea.WithinTimeSpan(new LatLon(34.1018, -118.2973), new TimeSpan(1, 0, 0), new RoutingStrategyForPedestrian()); /* calculate the convex polygon around all reachable points */ PolygonFence reachable = walkableInAnHour.ComputeConvexPolygon(); /* draw reachable area on map */ reachable.RenderBrush = new SolidBrush(Color.FromArgb(150, Color.Green)) rendererList.Add(reachable);
The different geofences representing a reachable area (convex PolygonFence, concave PolygonFence, or GeoFenceCollection of all reachable StreetLinkFences) are illustrated in the following screen shot:
The convex reachable area is shown in green, the concave reachable area is shown in blue, and all the reachable street link fences are shown in red.
IGeoFence - implement to create your own GeoFence
CircleFence - a circular GeoFence
PolygonFence - a multi-sided GeoFence
GeoFenceCollection - a collection of GeoFences which may be queried for a point inclusion/exclusion with a single call
RectangleFence - a rectangular GeoFence
StreetLinkFence - a GeoFence that extends to the boundary of a StreetLink. This can be used to test if a vehicle is traveling on a link.
ReachableArea - a class for calculating the geofence surrounding a region that can be reached within a specified time or distance.