Exemplo de como destacar uma região específica no mapa com Xamarin.Forms http://julianocustodio.com/regioes-maps-xamarin-forms
using UIKit; | |
using Xamarin.Forms; | |
using App.iOS; | |
using Xamarin.Forms.Maps.iOS; | |
using MapKit; | |
using Xamarin.Forms.Platform.iOS; | |
using CoreLocation; | |
using ObjCRuntime; | |
[assembly: ExportRenderer(typeof(global::App.CustomMap), typeof(CustomMapRenderer))] | |
namespace App.iOS | |
{ | |
public class CustomMapRenderer : MapRenderer | |
{ | |
MKPolygonRenderer polygonRenderer; | |
protected override void OnElementChanged(ElementChangedEventArgs<View> e) | |
{ | |
base.OnElementChanged(e); | |
if (e.OldElement != null) | |
{ | |
var nativeMap = Control as MKMapView; | |
if (nativeMap != null) | |
{ | |
nativeMap.RemoveOverlays(nativeMap.Overlays); | |
nativeMap.OverlayRenderer = null; | |
polygonRenderer = null; | |
} | |
} | |
if (e.NewElement != null) | |
{ | |
var formsMap = (global::App.CustomMap)e.NewElement; | |
var nativeMap = Control as MKMapView; | |
nativeMap.OverlayRenderer = GetOverlayRenderer; | |
CLLocationCoordinate2D[] coords = new CLLocationCoordinate2D[formsMap.ShapeCoordinates.Count]; | |
int index = 0; | |
foreach (var position in formsMap.ShapeCoordinates) | |
{ | |
coords[index] = new CLLocationCoordinate2D(position.Latitude, position.Longitude); | |
index++; | |
} | |
var blockOverlay = MKPolygon.FromCoordinates(coords); | |
nativeMap.AddOverlay(blockOverlay); | |
} | |
} | |
MKOverlayRenderer GetOverlayRenderer(MKMapView mapView, IMKOverlay overlayWrapper) | |
{ | |
if (polygonRenderer == null && !Equals(overlayWrapper, null)) | |
{ | |
var overlay = Runtime.GetNSObject(overlayWrapper.Handle) as IMKOverlay; | |
polygonRenderer = new MKPolygonRenderer(overlay as MKPolygon) | |
{ | |
FillColor = UIColor.Red, | |
StrokeColor = UIColor.Blue, | |
Alpha = 0.4f, | |
LineWidth = 9 | |
}; | |
} | |
return polygonRenderer; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment