I'm making renderers in my project, so that the PinType
enum will actually have an effect by changing the color of the pins. But whenever I use a non-default icon for the native Marker
, it draws as the default icon on top of my icon, with the default disappearing on click.
Here are some screenshots from using marker.SetIcon(BitmapDescriptorFactory.DefaultMarker(BitmapDescriptorFactory.HueGreen))
:
And here are some from using marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.ic_launcher))
(my app icon):
And here is my renderer code:
[assembly: ExportRenderer(typeof(ExtendedMap), typeof(ExtendedMapRenderer))] namespace eLationTechnician.Forms.Droid.Renderers { public class ExtendedMapRenderer : MapRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e) { base.OnElementChanged(e); if(e.NewElement != null) { UpdatePins(); } } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if(e.PropertyName == "Pins") { UpdatePins(); } } private void UpdatePins() { NativeMap.Clear(); foreach(var pin in Map.Pins) { var marker = new MarkerOptions() .SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude)) .SetTitle(pin.Label).SetSnippet(pin.Address); float hue = 0.0f; switch (pin.Type) { case PinType.Generic: case PinType.SearchResult: { hue = BitmapDescriptorFactory.HueRed; break; } case PinType.SavedPin: { hue = BitmapDescriptorFactory.HueGreen; break; } case PinType.Place: { hue = BitmapDescriptorFactory.HueBlue; break; } } marker.SetIcon(BitmapDescriptorFactory.DefaultMarker(hue)); NativeMap.AddMarker(marker); } } } }
Answers
An update: I have yet to determine where or why, but what's happening is a second set of markers is being drawn over the ones I place in my
UpdatePins
method. My pins are seemingly catching the click event and being put in the foreground when it occurs.You are using the Pins property to add your custom pin so, if you’re are adding one pin with myPCLCustomMap.Pins.Add(pin), Xamarin.Forms adds one with this method and another in your custom renderer.
You must use a custom property that isn’t Pins.
Even better - Set the Pin Icon from the PCL to save using custom renderers
Icon = SetPinIconStream("EmbeddedResource.FolderName.Image.png")