I use code in https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/map/
In the iOS,
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.Image will set all the pin to an identical image. I wonder how to set a different image to each pin.
Any ideas, help. I really appreciate it.
If you just change:
annotationView.Image = UIImage.FromFile ("pin.png");
to:
annotationView.Image = customPin.Image; // or whatever you called your image property
or:
annotationView.Image = UIImage.FromFile (customPin.Image); // if Image is a filename
then that should do it.
Given how obvious that is, I sense there's something in your question that I don't understand. As we've shown in multiple examples now, if you change the line that assigns a hard-coded image file so that it uses a property in your custom pin, then you'll get a different image. Is there something more to your question that we're not getting?
Answers
Hi,
Try with below code which works,
//if (annotationView == null)
//{
// annotationView = new CustomMKAnnotationView(annotation, customPin.Id);
@VarunBabu I believe this will set all the pins to same image right? How to show different pins on map at the same time?
No. That code will use a different image based on the value of
customPin.Id
, though it could be simplified a bit:@DaveHunt In my case, I have a lot of pins on the map, and each pin icon need show the the price of this place. So every pin is almost unique. Currently, I can generate each pin images, but I don't know how to sign each images to each pins.
You'll need to use a pin ID for each price. Then use the pin ID to assign an image just like the code above does. You could store the id<->image associations in a dictionary (say
pinToImage
) and use something like:@DaveHunt @VarunR
Thanks, That's real good point. My case is a little bit different. Please, see code.
"List customPins" have all the all pin location as well as image. For example, I have 100 pins in this List, in the annotationView.Image, is the any way sign one pin to one image respectively?
In android, there is marker( same as annotation in iOS), i can sign image to each marker, and add marker to map. I wonder how is that possible in iOS.
link is below:
https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/map/
If you just change:
to:
or:
then that should do it.
Given how obvious that is, I sense there's something in your question that I don't understand. As we've shown in multiple examples now, if you change the line that assigns a hard-coded image file so that it uses a property in your custom pin, then you'll get a different image. Is there something more to your question that we're not getting?
Thanks both @DaveHunt @VarunBabu
I am get confused with the way that android did. Yeah, this method can make the each pin in different image.
@DaveHunt I tried this way but not working can you help to find my mistake ?
'protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
MKAnnotationView annotationView = null;
this is my code