Hello guys, im stuck with a problem with the InfoWindowClicked event on Xamarin.Forms.Maps.
The event fires correctly on Android but don't fire on IOS (testing in simulator).
I made a simple CustomRenderer in order to change the pin icon, these are my files:
public class CustomMap : Map
{
public List CustomPins { get; set; }
}
public class DataPin : Pin
{
public string Icon { get; set; }
}
This is my custom renderer on IOS (pretty much the same of the doc):
public class CustomMapRenderer : MapRenderer
{
List<DataPin> customPins;
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
var nativeMap = Control as MKMapView;
customPins = formsMap.CustomPins;
nativeMap.GetViewForAnnotation = GetViewForAnnotation;
}
}
protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
if (annotation is MKUserLocation)
return null;
var customPin = GetCustomPin(annotation as MKPointAnnotation);
if (customPin == null)
{
return null;
}
MKAnnotationView annotationView = mapView.DequeueReusableAnnotation(customPin.Label);
if (annotationView == null)
{
annotationView = new MKAnnotationView(annotation, customPin.Label)
{
Image = UIImage.FromFile(customPin.Icon),
CalloutOffset = new CGPoint(0, 0)
};
}
annotationView.CanShowCallout = true;
return annotationView;
}
DataPin GetCustomPin(MKPointAnnotation pin)
{
if(customPins != null)
{
var position = new Position(pin.Coordinate.Latitude, pin.Coordinate.Longitude);
foreach (var customPin in customPins)
{
if (customPin.Position == position)
{
return customPin;
}
}
}
return null;
}
}
Here is the sample code where i add a pin with the InfoWindowClicked event (i removed some parts):
myfunc()
{
CustomMap map = new CustomMap()
{
IsShowingUser = true,
MapType = MapType.Street,
};
map.CustomPins = new List();
DataPin pin = new DataPin
{
Label = stop.Nome.Replace("_", " ").ToUpper(),
Type = PinType.Place,
Position = new Position(stop.Coordy, stop.CoordX),
Icon = "stop_pin.png"
};
pin.InfoWindowClicked += pinClickedEventHandler;
map.Pins.Add(pin);
map.CustomPins.Add(pin);
}
If i put a break inside the handler, only in android is executed while in IOS is ignored.
Do you have some suggestions about it? Im not sure if it is a custom renderer problem, since the event is not in the renderer, but maybe im missing something?
Thanks a lot for your help!