Xamarin Forms Maps - 瓷砖的定制

Xamarin Forms Maps - Customization of Tiles

我今天要来谈谈地图的瓷砖!

个人项目和公司项目,我需要定制很多Xamarin.Forms.Maps。我发现这个教程 Custom Map Tiles in Xamarin.Forms 只讲了 Android 和 iOS (又一次..)但是,我很想知道如何它适用于 WinPhone 8.1 and/or UWP。

另外,因为使用了Mapbox,请问这个项目真的make可以长期使用吗? (我只问那些对这个项目有所了解的人,因为我不知道如何阅读)。

据我所知,存在一些关于它的 nuget 包,但没有制作我真正想要的东西(我想在每个平台上自定义图块)

如果你有关于它的网站或者你已经这样做了,你能给我一些指导或任何帮助吗?谢谢!

编辑 1

我找到了 UWP 渲染器的这段代码,但它不会更改地图图块..

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace MapTileProject.UWP.Renderer
{
    public class CustomMapRenderer : MapRenderer
    {
        CustomMap customMap;
        MapControl mapControl;

        protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                customMap = e.NewElement as CustomMap;
                mapControl = Control as MapControl;

                UpdateTiles();
            }
        }

        private void UpdateTiles()
        {
            Debug.WriteLine("BEGINING !");
            HttpMapTileDataSource dataSource = new HttpMapTileDataSource(customMap.MapTileTemplate);
            MapTileSource tileSource = new MapTileSource(dataSource);
            mapControl.TileSources.Add(tileSource);
            Debug.WriteLine("END !");
        }
    }
}

found this code for the UWP renderer, but it doesn't change the map tiles

如果您使用 Fiddler 检查 Web 请求,您将看到请求 API URL 不正确:

参考 Overlay tiles from a tile source

UWP中标准的HttpMapTileDataSource应该是这样的:

http://www.web service name.com/z={zoomlevel}&x={x}&y={y}

它包括三个用于 X 和 Y 坐标和缩放级别的可替换参数:{zoomlevel}, {x}, {y}

所以我们需要先转换您的 MapTileTemplate 字符串:

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace MapTileProject.UWP.Renderers
{
    public class CustomMapRenderer : MapRenderer
    {
        CustomMap customMap;
        MapControl mapControl;

        protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                customMap = e.NewElement as CustomMap;
                mapControl = Control as MapControl;

                UpdateTiles();
            }
        }
        /// <summary>
        /// Convert MapTileTemplate string to fit UWP HttpMapTileDataSource
        /// </summary>
        /// <param name="mapTileTemplate"></param>
        /// <returns></returns>
        private string GetTileTemplateForUWP(string mapTileTemplate)
        {
            return mapTileTemplate.Replace("{z}", "{zoomlevel}");
        }

        private void UpdateTiles()
        {
            Debug.WriteLine("BEGINING !");
            HttpMapTileDataSource dataSource = new HttpMapTileDataSource(GetTileTemplateForUWP(customMap.MapTileTemplate));
            MapTileSource tileSource = new MapTileSource(dataSource);
            mapControl.TileSources.Add(tileSource);
            Debug.WriteLine("END !");
        }
    }

}

截图: