Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Custom renderer for Android Map like iOS map

$
0
0

Ok i have the following Map that need a custom renderer for Android and iOS:

public class MapViewModel : Map
    {
        public static readonly BindableProperty LocationsProperty = BindableProperty.Create<MapViewModel, List<Branch>>(x => x.Locations, new List<Branch>());
        public static readonly BindableProperty PinTappedCommandProperty = BindableProperty.Create<MapViewModel, Command>(x => x.PinTapped, null);

        public MapViewModel(List<Branch> locations)
        {
            Locations = locations;
            PinTapped = new Command(async (x) =>
                {
                    //await Navigation.PopModalAsync();
                    var info = new InformationView((Branch)x);
                    await Navigation.PushAsync(info);
                });
        }

        public List<Branch> Locations
        {
            get { return (List<Branch>)GetValue(LocationsProperty); }
            set { SetValue(LocationsProperty, value); }
        }

        public Command PinTapped 
        {
            get {return (Command) GetValue(PinTappedCommandProperty); }
            set {SetValue(PinTappedCommandProperty, value);}
        }
    }

And here is how I made the custom renderer for iOS:

public class MapViewiOS : ViewRenderer<MapViewModel, MKMapView>
    {
        private MKMapView NativeMap {get { return (this.NativeView as MapRenderer).Control as MKMapView; } }

        public MapViewiOS () : base()
        {
        }

        protected override void OnElementChanged (ElementChangedEventArgs<MapViewModel> e)
        {
            base.OnElementChanged (e);
            //potrebbe necessitare di controllo se null
            var map = e.NewElement;
            var cmd = map.PinTapped; // Send this along to MyMapDelegate
            var nativeMap = new MKMapView(); // Initiate with relevant parameters
            SetNativeControl (nativeMap);

            MyMapDelegate myMapDelegate = new MyMapDelegate (cmd); // Change constructor here
            nativeMap.Delegate = myMapDelegate;
            //---------------------------------Inserisco per ogni filiale il pin nella mappa
            foreach (Branch f in map.Locations) {
                nativeMap.AddAnnotation (new MyAnnotation (f));
            }
        }
    }

How can I implement it in Android? Which is the same object as MKMapView in Android?


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>