id | title | brief | article | sdk | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
316BE54A-5E70-440A-4B5E-4F32DAFF6224 |
Add an Overlay to a Map |
This recipe shows how to add an overlay to a map. |
|
|
An overlay lets you ‘draw’ over a map. To add an overlay to a MKMapView:
- Start with an existing
MKMapView
or review the Displaying a Location recipe.
- Declare class-level variables for the overlay and its renderer:
MKCircle circleOverlay;
MKCircleRenderer circleRenderer;
- Implement MKMapView.OverlayRenderer to provide a renderer for the overlay:
mapView.OverlayRenderer = (m, o) => {
if(circleRenderer == null) {
circleRenderer = new MKCircleRenderer(o as MKCircle);
circleRenderer.FillColor = UIColor.Purple;
circleRenderer.Alpha = 0.5f;
}
return circleRenderer;
};
- 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);
The OverlayRenderer
method must be assigned to prior to adding the MKOverlay
to the MKMapView
. If this is not done, the overlay will not appear until the map is moved or scaled in some way.
The MilesToLatitudeDegrees
and MilesToLongitudeDegrees
helper methods can be
found in the Displaying a Location recipe.