Hello,
In order to draw map polygons using Xamarin.Forms.Maps, I followed this nice article:
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/map/polygon-map-overlay/
It worked fine for UWP, but the OnElementChanged method for Android & iOS does not get called (N.B., the maps do work).
Everything I am using is up to date (Xamarin =4.5.0.486, Xamarin.Forms & Xamarin.Forms.Maps = 2.3.4.247, Xamarin.GooglePlayServices.Maps = 42.1021.1 etc.)
In android, forms and maps are initialised as required:
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
Xamarin.FormsMaps.Init(this, bundle);
LoadApplication(new App());
In App: MainPage = new MapPage();
Here is the renderer code:
Two things to note:
1. if "Map" in [assembly: ExportRenderer(typeof(Map)
is replaced with "CustomMap", I get the error ""Could not load type...PublicKeyToken=null while decoding custom attribute: (null)". This is the case for all my other custom renderers, but the others work fine using the Xamarin base classes (e.g., using "Button" instead of "CustomButton")
2. The example (see link above) uses "View" for the ElementChangedEventArgs, which not work for my project
using System.Collections.Generic;
using ProjectName.Droid.Renderers;
using Xamarin.Forms;
using ProjectName.Views;
using Xamarin.Forms.Maps.Android;
using Android.Gms.Maps;
using Xamarin.Forms.Maps;
using Android.Gms.Maps.Model;
using System.ComponentModel;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(Map), typeof(MapPolygonRenderer_Android))]
namespace ProjectName.Droid.Renderers
{
public class MapPolygonRenderer_Android : MapRenderer, IOnMapReadyCallback
{
private GoogleMap map;
private List<Position> shapeCoordinates;
protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
// Unsubscribe
}
if (e.NewElement != null && e.NewElement is CustomMap)
{
//Do stuff here
}
}
}
}
If anyone can shed some light on what the problem might be, or an alternative solution (e.g., using Dependency) I would be very grateful, as I am lost for ideas.
Cheers,
Phil