Created
October 11, 2017 15:31
-
-
Save juucustodio/37980167850b6addbc581866e06fa6c7 to your computer and use it in GitHub Desktop.
Exemplo de como destacar uma região específica no mapa com Xamarin.Forms http://julianocustodio.com/regioes-maps-xamarin-forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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