Xamarin MapView Tutorial

Given below is the source code Of a View Controller which has a MapView object. it shows 3 custom pins added by code.

there are lots of examples in bits and pieces but no one describes this simple excercise in clear steps.

 

using Foundation;
using System;
using UIKit;
using CoreLocation;
using CoreGraphics;
using MapKit;

namespace ResOp
{
    public partial class NearController : UIViewController
    {
        CLLocation currentLocation;
        public NearController (IntPtr handle) : base (handle)
        {
        }

        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);

            CLLocationManager locationManager = new CLLocationManager();

            mapView.MapType = MapKit.MKMapType.Standard;
            mapView.ShowsUserLocation = true;
            mapView.ZoomEnabled = true;
            //mapView.ShowsScale = true;
            mapView.ShowsUserLocation = false;

            currentLocation = locationManager.Location;
            Console.WriteLine(Nearby Location   + currentLocation.ToString());

            LocationManager lm = new LocationManager();
            lm.StartLocationUpdates();

            Console.WriteLine(initial loc: + mapView.UserLocation.Coordinate.Latitude + , + mapView.UserLocation.Coordinate.Longitude);
            mapView
                .ScrollEnabled = true;

            CLLocationCoordinate2D mapCenter = new CLLocationCoordinate2D(currentLocation.Coordinate.Latitude, currentLocation.Coordinate.Longitude);
            MKCoordinateRegion mapRegion = MKCoordinateRegion.FromDistance(mapCenter, 10000, 10000);
            mapView.CenterCoordinate = mapCenter;
            //mapRegion.Span = span;
            mapView.Delegate = new MapDelegate(this);

            mapView.Region = mapRegion;
            var span = new MKCoordinateSpan(MilesToLatitudeDegrees(25), MilesToLongitudeDegrees(25, currentLocation.Coordinate.Latitude));
            mapView.Region = new MKCoordinateRegion(mapCenter, span);

            //locationManager.RequestWhenInUseAuthorization();

            locationManager.StartUpdatingLocation();

            var annotationc = new BasicMapAnnotation(new CLLocationCoordinate2D(30.7333, 76.7794), Chandigarh, City beautiful);
            mapView.AddAnnotation(annotationc);

            var annotation = new BasicMapAnnotation(new CLLocationCoordinate2D(30.6942, 76.8606), Panchkula, City of Light);
            mapView.AddAnnotation(annotation);

            var annotationm = new BasicMapAnnotation(new CLLocationCoordinate2D(30.7046, 76.7179), Mohali, City of Light);
            mapView.AddAnnotation(annotationm);

        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Perform any additional setup after loading the view, typically from a nib
        }

        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that arent in use
        }

        public double MilesToLatitudeDegrees(double miles)
        {
            double earthRadius = 3960.0; // in miles
            double radiansToDegrees = 180.0 / Math.PI;
            return (miles / earthRadius) * radiansToDegrees;
        }
        public double MilesToLongitudeDegrees(double miles, double atLatitude)
        {
            double earthRadius = 3960.0; // in miles
            double degreesToRadians = Math.PI / 180.0;
            double radiansToDegrees = 180.0 / Math.PI;
            // derive the earths radius at that point in latitude
            double radiusAtLatitude = earthRadius * Math.Cos(atLatitude * degreesToRadians);
            return (miles / radiusAtLatitude) * radiansToDegrees;
        }

    }

    class MapDelegate : MKMapViewDelegate
    {
        protected string annotationIdentifier = BasicAnnotation;
        UIButton detailButton;
        NearController parent;

        public MapDelegate1(NearController parent)
        {
            this.parent = parent;
        }

        /// <summary>
        /// This is very much like the GetCell method on the table delegate
        /// </summary>
        public override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
        {

            // try and dequeue the annotation view
            MKAnnotationView annotationView = mapView.DequeueReusableAnnotation(annotationIdentifier);

            // if we couldnt dequeue one, create a new one
            if (annotationView == null)
                annotationView = new MKPinAnnotationView(annotation, annotationIdentifier);
            else // if we did dequeue one for reuse, assign the annotation to it
                annotationView.Annotation = annotation;
            annotationView.Image = (UIImage.FromBundle(events.png));
            // configure our annotation view properties
            annotationView.CanShowCallout = true;
            (annotationView as MKPinAnnotationView).AnimatesDrop = true;
            (annotationView as MKPinAnnotationView).PinColor = MKPinAnnotationColor.Green;
            annotationView.Selected = true;

            // you can add an accessory view, in this case, well add a button on the right, and an image on the left
            detailButton = UIButton.FromType(UIButtonType.DetailDisclosure);

            detailButton.TouchUpInside += (s, e) =>
            {
                Console.WriteLine(Clicked);
                //Create Alert
                var detailAlert = UIAlertController.Create(Annotation Clicked, You clicked on  +
                    (annotation as MKAnnotation).Coordinate.Latitude.ToString() + ,  +
                    (annotation as MKAnnotation).Coordinate.Longitude.ToString(), UIAlertControllerStyle.Alert);
                detailAlert.AddAction(UIAlertAction.Create(OK, UIAlertActionStyle.Default, null));
                parent.PresentViewController(detailAlert, true, null);
            };
            annotationView.RightCalloutAccessoryView = detailButton;

            annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromBundle(29_icon.png));
            //(annotationView as MKPinAnnotationView).Image = (UIImage.FromFile(29_icon.png));

            return annotationView;
        }

        // as an optimization, you should override this method to add or remove annotations as the 
        // map zooms in or out.
        public override void RegionChanged(MKMapView mapView, bool animated) { }
    }
}