none
Sample code for Bing Map control with a customized MapTileLayer

    Question

  • I am struggling for this issues for several days. I am building a Windows 8 Store App with Bing Map control RTM (C#.NET).  I'd like to set up a customized MapTileLayer so that I can render the map in Bing Map Restricted Countries. e.g. China. The following is my issue.

    The TileSource(String type in Win Store app) and GetTileUri event should be the key. When I add a customized MayTileLayer either by XAML or code without TileSource specified, it just give me an unspecified error in runtime. When I specify the TileSource, according to MSDN, the GetTileUri never fires.

    Occurs when a map tile is requested and returns the map tile URL.

    Do not use this callback event function if you are using the TileSource property.

    Could anyone kindly post a piece of sample code that can help me achieve the goal? It is to render the Map with Bing Map Control RTM version in C#.NET with the capability to send a customizable http request to render tile.

    Monday, October 22, 2012 1:18 PM

Answers

  • After about 3 minutes of playing around with the control I found this to work well:

    MapTileLayer layer2 = new MapTileLayer();
    layer2.GetTileUri += (s, e) =>
    {
        e.Uri = new Uri(string.Format("http://a.tile.openstreetmap.org/{0}/{1}/{2}.png", e.LevelOfDetail, e.X, e.Y));
    };
    
    MyMap.TileLayers.Add(layer2);


    http://rbrundritt.wordpress.com

    Wednesday, October 24, 2012 10:27 AM
  • Hi Richard,

    I think you're right! The key is that you must either specify the TileSource property or GetTileUri before adding the MapTileLayer instance to the map instance. otherwise, you'll get an unspecified error as I did.

    THanks man!

    • Marked as answer by Michael Han Wednesday, October 24, 2012 1:49 PM
    Wednesday, October 24, 2012 1:49 PM

All replies

  • Hi,

    I am getting the same issue. No idea how to make custom tile source working for windows 8 app. It is running pretty fine in wp7.

    Madeeha

    Tuesday, October 23, 2012 6:55 AM
  • I agree. It seems the Bing Map Control in WP7 provides much better flexibility to customize the tile. I am quite confused how shall we achieve the same goal by this version of Bing Map Control.
    Tuesday, October 23, 2012 7:26 AM
  • After about 3 minutes of playing around with the control I found this to work well:

    MapTileLayer layer2 = new MapTileLayer();
    layer2.GetTileUri += (s, e) =>
    {
        e.Uri = new Uri(string.Format("http://a.tile.openstreetmap.org/{0}/{1}/{2}.png", e.LevelOfDetail, e.X, e.Y));
    };
    
    MyMap.TileLayers.Add(layer2);


    http://rbrundritt.wordpress.com

    Wednesday, October 24, 2012 10:27 AM
  • Thank you for replying.

    But if you don't specify the "TileSource" property and you add the MapTileLayer instance to your map, you'll get an unspecified error in runtime.

    Wednesday, October 24, 2012 1:42 PM
  • Hi Richard,

    I think you're right! The key is that you must either specify the TileSource property or GetTileUri before adding the MapTileLayer instance to the map instance. otherwise, you'll get an unspecified error as I did.

    THanks man!

    • Marked as answer by Michael Han Wednesday, October 24, 2012 1:49 PM
    Wednesday, October 24, 2012 1:49 PM
  • The code I pasted came out a test app I created to answer this post. I never set the TileSource property and I see no errors. I'm using C# and referencing the RTM control and the C++ runtime. Here is my code:

    using Bing.Maps;
    using System;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Navigation;
    
    namespace CustomTileLayers
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
    
                MapTileLayer layer2 = new MapTileLayer();
                layer2.GetTileUri += (s, e) =>
                {
                    e.Uri = new Uri(string.Format("http://a.tile.openstreetmap.org/{0}/{1}/{2}.png", e.LevelOfDetail, e.X, e.Y));
                };
    
                MyMap.TileLayers.Add(layer2);
            }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
        }
    }
    

    And here is my XAML:

    <Page
        x:Class="CustomTileLayers.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:CustomTileLayers"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:m="using:Bing.Maps">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <m:Map Name="MyMap"/>
        </Grid>
    </Page>
    


    http://rbrundritt.wordpress.com

    Wednesday, October 24, 2012 1:49 PM
  • Hi Richard,

    if you wrote the code as following as I did, you'll get the unspecified error.

    MapTileLayer layer2 = new MapTileLayer();

     MyMap.TileLayers.Add(layer2);

                layer2
    .GetTileUri += (s, e) =>
               
    {
                    e
    .Uri = new Uri(string.Format("http://a.tile.openstreetmap.org/{0}/{1}/{2}.png", e.LevelOfDetail, e.X, e.Y));
               
    };

    But you resolved my issue indeed! There's dependency for the sequence of setting TileSource/GetTileUri and TileLayers.Add method.

    Wednesday, October 24, 2012 3:45 PM
  • Yes. you must set either the TileSource or the GetTileUri before adding the MapTileLayer to the Map.

    http://rbrundritt.wordpress.com

    Wednesday, October 24, 2012 4:07 PM