Bing Maps Layer

Download Samples Repository

Description

Demonstrates adding a Bing Maps layer to a Map in code.A Bing Maps key must be provided to view Bing Maps tiled map services.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.BingLayerSample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">
    <Grid>

        <esri:MapView>
            <esri:Map x:Name="map1" />
        </esri:MapView>

        <Border Name="BingKeyGrid" BorderBrush="Black" BorderThickness="1" Background="White" HorizontalAlignment="Right" VerticalAlignment="Top" 
                Margin="10">
            <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="10">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" 
                            Margin="2" >
                    <TextBlock FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" Text="Enter Bing Key" Margin="2" 
                               Foreground="Black" />
                    <Button Margin="2" Content="Get Bing Key" Click="Button_Click"/>
                </StackPanel>
                <TextBox x:Name="BingKeyTextBox" Width="250" HorizontalAlignment="Right" Margin="0,2,0,2" 
                        TextChanged="BingKeyTextBox_TextChanged" />
                <TextBlock Name="InvalidBingKeyTextBlock" Text="Invalid Key" Foreground="Red"
                        Margin="0,2,0,2" HorizontalAlignment="Center" Visibility="Collapsed"/>
                <Button Name="LoadMapButton" Content="Load Map" Width="100" Margin="0,5,0,10" IsEnabled="False" 
                        HorizontalAlignment="Center" Click="LoadMapButton_Click" />
            </StackPanel>
        </Border>

        <Border Name="LayerStyleGrid" BorderBrush="Black" BorderThickness="1" Background="White" HorizontalAlignment="Right" 
                VerticalAlignment="Top" Margin="10" Visibility="Collapsed">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="5">
                <RadioButton x:Name="RoadRadioButton" 
                         Tag="Road" IsChecked="true" Margin="5,0,0,0" 
                         GroupName="Layers" Content="Road" Click="RadioButton_Click"/>
                <RadioButton x:Name="AerialRadioButton" 
                         Tag="Aerial" Margin="5,0,0,0"  
                         GroupName="Layers" Content="Aerial" Click="RadioButton_Click"/>
                <RadioButton x:Name="AerialWithLabelsRadioButton" 
                         Tag="AerialWithLabels" Margin="5,0,0,0"  
                         GroupName="Layers" Content="Aerial - Labels" Click="RadioButton_Click"/>
            </StackPanel>
        </Border>

    </Grid>
</UserControl>
using Esri.ArcGISRuntime.Layers;
using System;
using System.Diagnostics;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// Demonstrates adding a Bing Maps layer to a Map in code.  A Bing Maps key must be provided to view Bing Maps tiled map services.
    /// </summary>
    /// <title>Bing Maps Layer</title>
	/// <category>Layers</category>
	/// <subcategory>Tiled Layers</subcategory>
	public partial class BingLayerSample : UserControl
    {
        public BingLayerSample()
        {
            InitializeComponent();
        }

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            string layerNameTag = (string)((RadioButton)sender).Tag;

            foreach (Layer layer in map1.Layers)
                if (layer is BingLayer)
                    layer.IsVisible = false;
            
            map1.Layers[layerNameTag].IsVisible = true;
        }

        private void BingKeyTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if ((sender as TextBox).Text.Length >= 64)
                LoadMapButton.IsEnabled = true;
            else
                LoadMapButton.IsEnabled = false;
        }

        private void LoadMapButton_Click(object sender, RoutedEventArgs e)
        {
            WebClient webClient = new WebClient();
            string uri = string.Format("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?supressStatus=true&key={0}", BingKeyTextBox.Text);

            webClient.OpenReadCompleted += (s, a) =>
            {
                if (a.Error == null)
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(BingAuthentication));
                    BingAuthentication bingAuthentication = serializer.ReadObject(a.Result) as BingAuthentication;
                    a.Result.Close();
                    string authenticationResult = bingAuthentication.AuthenticationResultCode.ToString();

                    if (authenticationResult == "ValidCredentials")
                    {
                        foreach (BingLayer.LayerType layerType in (BingLayer.LayerType[])Enum.GetValues(typeof(BingLayer.LayerType)))
                        {
                            BingLayer bingLayer = new BingLayer()
                            {
                                ID = layerType.ToString(),
                                MapStyle = layerType,
                                Key = BingKeyTextBox.Text,
                                IsVisible = false
                            };

                            map1.Layers.Add(bingLayer);
                        }

                        map1.Layers[0].IsVisible = true;

                        BingKeyGrid.Visibility = System.Windows.Visibility.Collapsed;
                        LayerStyleGrid.Visibility = System.Windows.Visibility.Visible;

                        InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Collapsed;

                    }
                    else InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Visible;
                }
                else InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Visible;
            };

            webClient.OpenReadAsync(new System.Uri(uri));
        }

        [DataContract]
        public class BingAuthentication
        {
            [DataMember(Name = "authenticationResultCode")]
            public string AuthenticationResultCode { get; set; }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("IExplore.exe", "https://www.bingmapsportal.com");
        }

    }
}
Feedback on this topic?