user3841879
user3841879

Reputation: 659

Xamarin.Forms - MKMapView iOS Render

I tried using Xamarin.Forms.Maps, and it is missing many features, so I decided to make an iOS ViewRenderer with the MKMapView and adding custom pin images and such, but I am not sure how to make a ViewRenderer and how MKMapView works. Can someone be kind enough to show me how it works and give me a small quick example of just showing the map.

I just want to make a custom render for ios that will show MKMapView, the rest i can probably figure out, but I cant even figure out how to make it show from the viewrenderer.

Upvotes: 1

Views: 2078

Answers (1)

Rui Marinho
Rui Marinho

Reputation: 1712

Simple example on how to build your Custom ViewRenderer

in your PCL project create something that inehrits from view:

public class CustomMap: View
{

    public static readonly BindableProperty PinsItemsSourceProperty = BindableProperty.Create ("PinsItemsSource ", typeof(IEnumerable), typeof(CustomMap), null, BindingMode.OneWay, null, null, null, null);

public IEnumerable PinsItemsSource {
        get {
            return (IEnumerable)base.GetValue (CustomMap.PinsItemsSourceProperty );
        }
        set {
            base.SetValue (CustomMap.PinsItemsSourceProperty , value);
        }
    }

}

Then on your IOS create your custom renderer for that view like so:

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer ))]
namespace Xamarin.Forms.Labs.iOS.Controls
{
    public class CustomMapRenderer : ViewRenderer<CustomMap,MKMapView >
    {
        protected override void OnElementChanged (ElementChangedEventArgs<CustomMap> e)
        {
            base.OnElementChanged (e);
            var mapView = new MKMapView (this.Bounds);
            mapView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
            foreach(item in e.NewElement.PinsItemsSource )
            {
               //add the points 
               var annotation = new BasicMapAnnotation (new CLLocationCoordinate2D(x,y), "something", "something");
               mapView.AddAnnotation(annotation);

            }
            base.SetNativeControl(mapView);

        }
}

ps: i wrote this code on the fly from my head and i didn't tested but it should help you get going

Upvotes: 2

Related Questions