Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
..
Failed to load latest commit information.
Add_an_Annotation
Images
README.md

README.md

id title brief article sdk
E5EEB812-C303-3B50-5C05-929C66B41390
Add an Annotation to a Map
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.