Xamarin.Forms - MKMapView iOS Render

I tried using Xamarin.Forms.Maps and it has many functions, so I decided to make iOS ViewRenderer using MKMapView and add custom pin images, etc., but I'm not sure how to make ViewRenderer and how MKMapView Works. Can someone be kind enough to show me how this works and give me a small quick example of how to just show a map.

I just want to do an individual rendering for ios that MKMapView will show, the rest I can probably figure out, but I can't even figure out how to make this show from viewrenderer.

+1
source share
1 answer

A simple example of creating a custom ViewRenderer

in your PCL project, create something that infers from the 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 own renderer for this view as follows:

 [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 have not tested it, but it should help you get away.

+2
source

Source: https://habr.com/ru/post/973999/


All Articles