Reputation: 2783
I am writing a Xamarin Forms application which implements a custom map renderer for Android and iOS. I would like to implement custom pins, where different colours mean different things on my map.
My code for adding pins to my custom map is here:
List<BasicMapAnnotation> annotationList = new List<BasicMapAnnotation>();
for (int i = 0; i < userLocations.Count; i++)
{
LatLong latLongs = JsonConvert.DeserializeObject<LatLong>(userLocations[i].coords);
var annotation = new BasicMapAnnotation(new CLLocationCoordinate2D(Convert.ToDouble(latLongs.latitude), Convert.ToDouble(latLongs.longitude)), userLocations[i].user_id, "Latitude: " + latLongs.latitude.ToString() + " Longitude: " + latLongs.longitude.ToString());
annotationList.Add(annotation);
}
nativeMap.AddAnnotations(annotationList.ToArray());
How do I now add some custom login when drawing my pins, such that they are different colours?
if(something())
{
annotation.colour = Color.Green;
}
Upvotes: 1
Views: 668
Reputation: 6953
I believe that on iOS you can set the PinTintColor but on Android you have to set a bitmap for the pin.
Have a look at the source code for TK.Custom map on GitHub in particular the custom renderers. They show how to change the color of the pins.
Here is code from the iOS custom renderer showing changing a pin color.
var pinAnnotationView = annotationView as MKPinAnnotationView;
if (pinAnnotationView != null)
{
pinAnnotationView.AnimatesDrop = AnimateOnPinDrop;
var pinTintColorAvailable = pinAnnotationView.RespondsToSelector(new Selector("pinTintColor"));
if (!pinTintColorAvailable)
{
return;
}
if (pin.DefaultPinColor != Color.Default)
{
pinAnnotationView.PinTintColor = pin.DefaultPinColor.ToUIColor();
}
else
{
pinAnnotationView.PinTintColor = UIColor.Red;
}
}
Upvotes: 2