Silverlight学习笔记十七BingMap(六)之ImageryService的应用
时间:2010-08-23 来源:永恒的记忆
BIngMap的ImageryService服务是一个微软发布的WCF服务,它用来获取图片系统的图片信息
本例中使用的是中文图片系统
效果如图
  
一、获取中文图片系统类(ChinaTileSource.cs)
    public class ChinaTileSource
      {
          /// <summary>
          /// 加载中国地图系统
          /// http://r2.tiles.ditu.live.com/tiles/r{quadkey}.png?g=41中国地图系统
          /// </summary>
          /// <returns>TileSource</returns>
          public TileSource GetChinaTileSource()
          {
              UriBuilder tileSourceUri = new UriBuilder("http://r2.tiles.ditu.live.com/tiles/r{quadkey}.png?g=41");
              MapTileLayer tileLayer = new MapTileLayer();
              LocationRectTileSource tileSource = new LocationRectTileSource(tileSourceUri.Uri.ToString()
                 , new LocationRect(new Location(60, 60), new Location(13, 140)), new Range<double>(1, 16));
              return tileSource;
          }
      }
二、使用中文图片系统
   /// <summary>
          /// 中文图片系统
          /// </summary>
          public ImageryServiceDemo()
          {
              InitializeComponent();
              MapTileLayer tileLayer = new MapTileLayer();
              ChinaTileSource gts = new ChinaTileSource();
              tileLayer.TileSources.Add(gts.GetChinaTileSource());
              myMap.Children.Add(tileLayer);
          }
三.获取图片信息
1.前台
  <UserControl x:Class="SlBindMapDemo.ImageryServiceDemo"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:map="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"  
      mc:Ignorable="d"
      d:DesignHeight="300" d:DesignWidth="800">
     
      <Grid x:Name="LayoutRoot" Background="White">
          <map:Map  x:Name="myMap" Grid.Row="0"  Height="300"  Width="400" Center="33.845881352,105.165628188471"
         NavigationVisibility="Collapsed" Mode="Road" CredentialsProvider="AkGGA_JlwP7XGV8JxIPb8oEWxrInlLMGKpCe7QM4QB5cg4UGNCqUyjqVfC0B2-XC" >
          </map:Map>
          <Border BorderBrush="Gray" BorderThickness="3,3,3,3"
              Margin="3,3,3,3" HorizontalAlignment="Right"
              Opacity="0.78"  Height="160" Background="#A82D2D2D"
              VerticalAlignment="Top" CornerRadius="5,5,5,5">
              <StackPanel Orientation="Vertical">
                  <StackPanel Orientation="Horizontal" Margin="3,3,3,3">
                      <TextBlock Text="经度:"></TextBlock>
                      <TextBox x:Name="tbLongitude" Width="130"></TextBox>
                      <TextBlock Text="纬度:"></TextBlock>
                      <TextBox x:Name="tbLatitude" Width="130"></TextBox>
                      <TextBlock Text="缩放级别:"></TextBlock>
                      <TextBox x:Name="tbZoomLevel" Width="30"></TextBox>
                      <Button x:Name="btnImageMetadata" Click="btnImageMetadata_Click" Content="获取图片映射地址" Margin="3"></Button>
                  </StackPanel>
                  <StackPanel Orientation="Horizontal" Margin="3,3,3,3">
                      <TextBlock Text="映射地址:"></TextBlock>
                      <TextBox x:Name="tbMetadataResult" Width="500"></TextBox>
                  </StackPanel>
                  <StackPanel Orientation="Horizontal" Margin="3,3,3,3">
                      <TextBlock Text="图片高度:"></TextBlock>
                      <TextBox x:Name="tbHeight" Width="150"></TextBox>
                      <TextBlock Text="图片宽度:"></TextBlock>
                      <TextBox x:Name="tbWidth" Width="150"></TextBox>
                      <Button Content="生产图片" Click="btnGetUrl_Click"></Button>
                  </StackPanel>
                  <StackPanel Orientation="Horizontal">
                      <Image x:Name="imgMap"></Image>
                  </StackPanel>
              </StackPanel>
          </Border>
      </Grid>
  </UserControl>
2.后台
    /// <summary>
          /// 获取图片地址
          /// </summary>
          /// <param name="sender"></param>
          /// <param name="e"></param>
          private void btnImageMetadata_Click(object sender, RoutedEventArgs e)
          {
              //构造服务请求对象
              var request = new ImageryMetadataRequest();
              request.Credentials = new Credentials();
              request.Credentials.ApplicationId = "AkGGA_JlwP7XGV8JxIPb8oEWxrInlLMGKpCe7QM4QB5cg4UGNCqUyjqVfC0B2-XC";
              //设置地理位置经度、纬度和地图缩放级别,从界面控件收集值
              var location = new Location(double.Parse(this.tbLatitude.Text), double.Parse(this.tbLongitude.Text));
              request.Options = new ImageryMetadataOptions();
              request.Options.Location = location;
              request.Options.ZoomLevel = int.Parse(this.tbZoomLevel.Text);
              request.Style = MapStyle.Road;
              //构造ImageryService客户端代理对象实例
              var client = new ImageryServiceClient();
              client.GetImageryMetadataCompleted += (o, args) =>  //处理请求的响应接口
              {
                  if (args.Error == null)
                  {
                      var response = args.Result;
                      this.tbMetadataResult.Text = response.Results[0].ImageUri.ToString();
                      this.tbHeight.Text = response.Results[0].ImageSize.Height.ToString();
                      this.tbWidth.Text = response.Results[0].ImageSize.Width.ToString();
                  }
              };
              //发起异步调用
              client.GetImageryMetadataAsync(request);
          }
  
  四、生成图片
1.前台
我在这里定义了一个Image用它来存放生成的图片
   <StackPanel Orientation="Horizontal">
                      <Image x:Name="imgMap"></Image>
                  </StackPanel>
1.后台
   /// <summary>
          /// 生成图片
          /// </summary>
          /// <param name="sender"></param>
          /// <param name="e"></param>
          private void btnGetUrl_Click(object sender, RoutedEventArgs e)
          {
              var request = new MapUriRequest();
              request.Credentials = new Microsoft.Maps.MapControl.Credentials();
      //申请的BIngMap ID
              request.Credentials.ApplicationId = "AkGGA_JlwP7XGV8JxIPb8oEWxrInlLMGKpCe7QM4QB5cg4UGNCqUyjqVfC0B2-XC";
              request.Center = new Location(35.0521567883562, 100.81816585252);
              var opt = new MapUriOptions();
      //图片的模式
              opt.Style = MapStyle.AerialWithLabels;
              opt.ZoomLevel = 4;
              opt.ImageSize = new SizeOfint();
              opt.ImageSize.Height = int.Parse(tbHeight.Text);
              opt.ImageSize.Width = int.Parse(tbWidth.Text);
request.Options = opt;
              var client = new ImageryServiceClient();
              client.GetMapUriCompleted += (o, args) =>
              {
                  var response = args.Result;
                  imgMap.Source = new BitmapImage(new Uri(response.Uri, UriKind.RelativeOrAbsolute));
              };
              client.GetMapUriAsync(request);
          }










