I need a map that displays a customized callout when a pin is touched. I found and used the example found here:
The example works fine. I need to modify it so the view displayed is a Xamarin.Forms ContentView. I found this example to convert a Forms ContentView to a native view:
https://github.com/rid00z/LoadingFormsViewFromNative
This code works great as well, in isolation. Using this method to produce the view used as the pin’s callout works fine in Android. However, using this method to produce the view in iOS has an issue. The UIView is created and can be viewed, but when it’s set as the DetailCalloutAccessoryView, the Callout bubble does not resize to display the UIView. However, when I use a natively-build UIView, the callout bubble is sized properly.
The following method should hopefully provide enough insight into the issue:
MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
try
{
MKAnnotationView annotationView = null;
if (annotation is MKUserLocation) return null;
var anno = annotation as MKPointAnnotation;
if (anno == null) return null;
var fieldbookPin = GetCustomPin(anno);
if (fieldbookPin == null)
{
throw new Exception("Custom pin not found");
}
annotationView = mapView.DequeueReusableAnnotation(((Entity)fieldbookPin.entity).getPrimaryKeyValue().ToString());
if (annotationView == null)
{
annotationView = new MKAnnotationView(annotation, ((Entity)fieldbookPin.entity).getPrimaryKeyValue().ToString());
switch (fieldbookPin.entity.GetType().Name)
{
case "CM_VISITOR":
CM_VISITOR visitor = (CM_VISITOR)fieldbookPin.entity;
VisitorPinView newView = new VisitorPinView(visitor);
annotationView.DetailCalloutAccessoryView = FormsToNativeIos.ConvertFormsToNative(newView, new CGRect(0, 0, newView.WidthRequest, newView.HeightRequest));
//UIImageView childView = new UIImageView();
//childView.Image = UIImage.FromFile("Red.png");
//annotationView.DetailCalloutAccessoryView = childView;
annotationView.Image = UIImage.FromFile("Green.png");
break;
case "CM_DIARY":
annotationView.Image = UIImage.FromFile("Red.png");
break;
default:
annotationView.Image = UIImage.FromFile("Blue.png");
break;
}
annotationView.CalloutOffset = new CGPoint(0, 0);
}
annotationView.CanShowCallout = true;
return annotationView;
}
catch (FieldbookException ex)
{
throw ex;
}
catch (Exception ex)
{
FieldbookException fbException = new FieldbookException(ex);
fbException.severity = FieldbookExceptionSeverity.USER_MESSAGE;
fbException.userMessage = "An error occurred in MapFBRenderer.GetViewForAnnotation. Please exit the application and start over.";
throw fbException;
}
}
When I remark out the line that calls FormsToNativeIos.ConvertFormsToNative and unremark the following three lines, the Callout bubble is sized properly to display the UIImageView.
I created a small test Xamarin Forms ContentView and used it instead of the normal ContentView and while the Callout bubble does not resize, I can see the image on this test View in the Callout bubble.