Add an Annotation to a Map

PDF for offline use
Sample Code:
Related Articles:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

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

Recipe

A basic annotation consists of a pin marker added to map. When touched, the pin displays a small popup with more information. To add an annotation to a MKMapView:

  1. Start with an existing MKMapView or review the Displaying a Location (MapView) recipe.
  1. Create a subclass of MKAnnotation
class BasicMapAnnotation : MKAnnotation
{
        CLLocationCoordinate2D coord;
        string title, subtitle;

        public override CLLocationCoordinate2D Coordinate { get{ return coord; }}
        public override void SetCoordinate(CLLocationCoordinate2D value) 
    {
            coord = value;
        }
        public override string Title { get{ return title; }}
        public override string Subtitle { get{ return subtitle; }}
        public BasicMapAnnotation (CLLocationCoordinate2D coordinate, string title, string subtitle) 
    {
            this.coord = coordinate;
            this.title = title;
            this.subtitle = subtitle;
        }
}
  1. Create the annotation and add it to the map:
var annotation = new BasicMapAnnotation (new CLLocationCoordinate2D(48.857,2.351), "Paris", "City of Light");
mapView.AddAnnotation(annotation);
  1. Finally, ensure the map is centered to show the annotation:
var coords = new CLLocationCoordinate2D(48.857,2.351);
var span = new MKCoordinateSpan(MilesToLatitudeDegrees(2), MilesToLongitudeDegress(2, coords.Latitude));
mapView.Region = new MKCoordinateRegion(coords, span);

Additional Information

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

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.