plasma
plasma

Reputation: 297

How to set a different image for each pin? Xamarin iOS

I stop there for a while, I wonder someone can help me out. We know that MKAnnotationView.Image = some image, will set all the pin to this image. How to set different image for different pins. Usually, there is for loop to sign one pin to one image, but with MKAnnotationView.Image, how to set it.

MKAnnotationView GetViewForAnnotation (MKMapView mapView, IMKAnnotation     annotation)
{
  MKAnnotationView annotationView = null;

  if (annotation is MKUserLocation)
    return null;

  var anno = annotation as MKPointAnnotation;
  var customPin = GetCustomPin (anno);
  if (customPin == null) {
    throw new Exception ("Custom pin not found");
  }

  annotationView = mapView.DequeueReusableAnnotation (customPin.Id);
  if (annotationView == null) {
    annotationView = new CustomMKAnnotationView (annotation, customPin.Id);
    annotationView.Image = UIImage.FromFile ("pin.png");
    annotationView.CalloutOffset = new CGPoint (0, 0);
    annotationView.LeftCalloutAccessoryView = new UIImageView (UIImage.FromFile ("monkey.png"));
    annotationView.RightCalloutAccessoryView = UIButton.FromType (UIButtonType.DetailDisclosure);
    ((CustomMKAnnotationView)annotationView).Id = customPin.Id;
    ((CustomMKAnnotationView)annotationView).Url = customPin.Url;
  }
  annotationView.CanShowCallout = true;

  return annotationView;
}

annotationView.Image = UIImage.FromFile ("pin.png"); will set all pin to this image. However, usually people want different image for each pin. Any idea, I do really appreciate it.

This is link for this code https://github.com/xamarin/xamarin-forms-samples/blob/master/CustomRenderers/Map/iOS/CustomMapRenderer.cs

offical guild Customizing a Map is there https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/map/

Upvotes: 1

Views: 1328

Answers (1)

RoundTwo
RoundTwo

Reputation: 95

This is what I found in the Xamarin forums.

annotationView.Image = UIImage.FromFile ("pin.png");

if you have any image, just sign image into annotationView.Image. You can store the image identity in my customized pin. So you just put image identity to point out right image you want use.

Something like annotationView.Image = UIImage.FromFile (customPin.Image) Is this something you want?

Upvotes: 1

Related Questions