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?