Cross Platform
Android
iOS
 

Add an Overlay to a Map

This recipe shows how to add an annotation to a map.

Recipe

An overlay lets you ‘draw’ over a map. To add an overlay to a MKMapView:

  1. Start with an existing MKMapView or review the Displaying a Location recipe.
  2. Declare class-level variables for the overlay and its view:
MKCircle circleOverlay;
MKCircleView circleView;
  1. Create an overlay, in this case a circle positioned near the Pyramids of Giza, and add it to the map:
var coords = new CLLocationCoordinate2D(29.976111, 31.132778); //giza
circleOverlay = MKCircle.Circle (coords, 200);
mapView.AddOverlay (circleOverlay);
  1. Implement MKMapView.GetViewForOverlay to provide a view for the overlay:
mapView.GetViewForOverlay = (m, o) => {
    if(circleView == null) {
        circleView = new MKCircleView(o as MKCircle);
        circleView.FillColor = UIColor.Purple;
        circleView.Alpha = 0.5f;
    }
    return circleView;
};

Additional Information

The MilesToLatitudeDegrees and MilesToLongitudeDegrees helper methods can be found in the Displaying a Location recipe.