Cross Platform
Android
iOS
 

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.
  2. Create a subclass of MKAnnotation
class BasicMapAnnotation : MKAnnotation{
    public override CLLocationCoordinate2D Coordinate {get;set;}
    string title, subtitle;
    public override string Title { get{ return title; }}
    public override string Subtitle { get{ return subtitle; }}
    public BasicMapAnnotation (CLLocationCoordinate2D coordinate, string title, string subtitle) {
        this.Coordinate = 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.