Hi! I',m developing an application in Xamarin Forms and based on the examples of Xamarin Forms i did this:
https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/maps/map-overlay/polyline/
And I modify the CustomMapRender.cs class in android project:
using System.Collections.Generic;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using MapOverlay;
using MapOverlay.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Maps.Android;
using System.Threading.Tasks;
[assembly:ExportRenderer (typeof(CustomMap), typeof(CustomMapRenderer))]
namespace MapOverlay.Droid
{
public class CustomMapRenderer : MapRenderer, IOnMapReadyCallback
{
GoogleMap map;
List routeCoordinates;
protected override void OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs<View> e)
{
base.OnElementChanged (e);
if (e.OldElement != null) {
// Unsubscribe
}
if (e.NewElement != null) {
var formsMap = (CustomMap)e.NewElement;
routeCoordinates = formsMap.RouteCoordinates;
((MapView)Control).GetMapAsync (this);
}
}
public void OnMapReady (GoogleMap googleMap)
{
map = googleMap;
var polylineOptions = new PolylineOptions ();
polylineOptions.InvokeColor (0x66FF0000);
foreach (var position in routeCoordinates) {
polylineOptions.Add (new LatLng (position.Latitude, position.Longitude));
await Task.Delay(30);
}
map.AddPolyline (polylineOptions);
}
}
}
In Android, the Delay in the foreach animate the polyline, and the idea is that animating the polyline you can see the direction of the route.
But, I want to do the same in iOS project, but I don't know how to implement the same idea. I have the code that show the route, is the same class, CustomMapRenderer.cs
namespace MapOverlay.iOS
{
public class CustomMapRenderer : MapRenderer
{
MKPolylineRenderer polylineRenderer;
protected override void OnElementChanged (ElementChangedEventArgs<View> e)
{
base.OnElementChanged (e);
if (e.OldElement != null) {
var nativeMap = Control as MKMapView;
nativeMap.OverlayRenderer = null;
}
if (e.NewElement != null) {
var formsMap = (CustomMap)e.NewElement;
var nativeMap = Control as MKMapView;
nativeMap.OverlayRenderer = GetOverlayRenderer;
CLLocationCoordinate2D[] coords = new CLLocationCoordinate2D[formsMap.RouteCoordinates.Count];
int index = 0;
foreach (var position in formsMap.RouteCoordinates) {
coords [index] = new CLLocationCoordinate2D (position.Latitude, position.Longitude);
index++;
}
var routeOverlay = MKPolyline.FromCoordinates (coords);
nativeMap.AddOverlay (routeOverlay);
}
}
MKOverlayRenderer GetOverlayRenderer (MKMapView mapView, IMKOverlay overlay)
{
if (polylineRenderer == null) {
polylineRenderer = new MKPolylineRenderer (overlay as MKPolyline);
polylineRenderer.FillColor = UIColor.Blue;
polylineRenderer.StrokeColor = UIColor.Red;
polylineRenderer.LineWidth = 3;
polylineRenderer.Alpha = 0.4f;
}
return polylineRenderer;
}
}
}
Any idea of how I can do this?
Thank you and regards from Mexico.