Custom pin on xamarin forms map

I have a Xamarin Forms project which requires a custom map pin. I have the stub defined in the PCL with a couple of additional properties (for lat and long) defined within the class that inherits the map.

The map shows up fine, but the custom image pin only shows as the standard red pin.

Below is my custom renderer for the map (iOS version). From what I can see on various forums, this should work fine.

public class CustomMapRenderer : ViewRenderer<CustomMap, MKMapView>
{
    MKMapView mkMapView;

    protected override void OnElementChanged(ElementChangedEventArgs<CustomMap> e)
    {
        base.OnElementChanged(e);
        var map = e.NewElement;

        SetNativeControl(new MKMapView(CGRect.Empty));
        mkMapView = Control;

        MyMapDelegate myMapDelegate = new MyMapDelegate();
        mkMapView.Delegate = myMapDelegate;
        mkMapView.AddAnnotation(new MKPointAnnotation()
            {
                Coordinate = new CLLocationCoordinate2D(map.MapPinLatitude, map.MapPinLongitude)
            });
        mkMapView.MapType = MKMapType.Hybrid;
        mkMapView.ZoomEnabled = true;
    }
}

public class MyMapDelegate : MKMapViewDelegate
{
    protected string annotationIdentifier = "PinAnnotation";

    public override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
    {

        MKAnnotationView anView;

        if (annotation is MKUserLocation)
            return null; 

        // create pin annotation view
        anView = (MKPinAnnotationView)mapView.DequeueReusableAnnotation(annotationIdentifier);

        if (anView == null)
            anView = new MKPinAnnotationView(annotation, annotationIdentifier);

        anView.Image = GetImage("pinned_location.png");
        anView.CanShowCallout = true;

        return anView;
    }

    public UIImage GetImage(string imageName)
    {
        var image = UIImage.FromFile(imageName).Scale(new SizeF() { Height = 20, Width = 30 });

        return image;
    }

Can anyone suggest why I'm not seeing the custom image for the pin?

Answers


You should use MKAnnotationView instead of MKPinAnnotationView.

// create annotation view
anView = (MKAnnotationView)mapView.DequeueReusableAnnotation(annotationIdentifier);

if (anView == null)
    anView = new MKAnnotationView(annotation, annotationIdentifier);

MKPinAnnotationView is only for default icon.


Need Your Help

Android app black screen ( onCreate() )

java android adt oncreate

In my app, I am trying to get my activity to change text on a button. When I call the ansGen() method in onCreate() , I get a black screen.

Typesafe Javascript

javascript compiler-construction interface type-safety

Is there a way to enforce types in JavaScript? I'm thinking of a pre-processor which takes an input file written in ActionScript 3 or Java and converts it to JS.