Skip to content

Instantly share code, notes, and snippets.

@blounty
Created February 17, 2015 04:59
Show Gist options
  • Save blounty/aeb7fbb850d7fe5455e7 to your computer and use it in GitHub Desktop.
Save blounty/aeb7fbb850d7fe5455e7 to your computer and use it in GitHub Desktop.
Create mapping apps that have amazingly cool custom map tiles with Xamarin.Forms
using System;
using Xamarin.Forms.Maps.Android;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Xamarin.Forms;
using CustomMapTiles;
using CustomMapTiles.Droid.Renderers;
[assembly: ExportRenderer (typeof (CustomMap), typeof (CustomMapRenderer))]
namespace CustomMapTiles.Droid.Renderers
{
public class CustomMapRenderer
: MapRenderer
{
MapView map;
CustomTileProvider tileProvider;
CustomMap customMap;
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged(e);
if (e.OldElement == null) {
map = Control as MapView;
customMap = e.NewElement as CustomMap;
var tileProvider = new CustomTileProvider (512, 512, customMap.MapTileTemplate);
var options = new TileOverlayOptions().InvokeTileProvider(tileProvider);
map.Map.AddTileOverlay (options);
}
}
}
}
using System;
using Android.Gms.Maps.Model;
namespace CustomMapTiles.Droid.Renderers
{
public class CustomTileProvider : UrlTileProvider
{
string urlTemplate;
public CustomTileProvider (int x, int y, string urlTemplate)
: base(x,y)
{
this.urlTemplate = urlTemplate;
}
public override Java.Net.URL GetTileUrl (int x, int y, int z)
{
var url = urlTemplate.Replace("{z}", z.ToString()).Replace("{x}", x.ToString()).Replace("{y}", y.ToString());
Console.WriteLine (url);
return new Java.Net.URL (url);
}
}
}
using System;
using Xamarin.Forms.Maps;
namespace CustomMapTiles
{
public class CustomMap
: Map
{
public string MapTileTemplate
{
get;
set;
}
}
}
using System;
using Xamarin.Forms.Maps.iOS;
using Xamarin.Forms;
using CustomMapTiles;
using CustomMapTiles.iOS.Renderers;
using MapKit;
[assembly: ExportRenderer (typeof (CustomMap), typeof (CustomMapRenderer))]
namespace CustomMapTiles.iOS.Renderers
{
public class CustomMapRenderer
: MapRenderer
{
MKMapView mapView;
CustomMap customMap;
protected override void OnElementChanged(Xamarin.Forms.Platform.iOS.ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{
mapView = Control as MKMapView;
customMap = e.NewElement as CustomMap;
var overlay = new MKTileOverlay (customMap.MapTileTemplate);
overlay.CanReplaceMapContent = false;
overlay.GeometryFlipped = false;
mapView.AddOverlay (overlay, MKOverlayLevel.AboveLabels);
mapView.OverlayRenderer = (mv, o) =>
new MKTileOverlayRenderer((MKTileOverlay)o);
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CustomMapTiles.Views.MapPage"
xmlns:local="clr-namespace:CustomMapTiles;assembly=CustomMapTiles">
<local:CustomMap x:Name="MyMap"
MapTileTemplate="http://api.tiles.mapbox.com/v4/blounty.l7lok06j/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiYmxvdW50eSIsImEiOiJYanJxSGxJIn0.LV0GHzqOvtGSyV6CjHqfuw"/>
</ContentPage>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment