Virtual Earth Silverlight Map Control将Virtual Earth融入了Silverlight,使開發者不必再去使用大量麻煩的Javascript API來開發Virtual Earth應用了。除此之外,它還具有以下優點:
對資料和地圖進行動畫開發;
可縮放的界面,包括将來的移動版;
深入整合視訊、音頻和圖像,不僅僅是彈出層;
支援自定義主題的地圖控件;
跨浏覽器支援,不再需要考慮 CSS Hack 和跨浏覽器的 JavaScript 問題。
這個控件的應用非常簡單。下面我們就打開Visual Studio 2008,随便試試吧!
第二步:添加對Microsoft.VirtualEarth.MapControl.dll的引用。
Microsoft.VirtualEarth.MapControl.dll位于Virtual Earth Silverlight Map Control (CTP) SDK安裝路徑内。
<a href="https://images.cnblogs.com/cnblogs_com/azure/WindowsLiveWriter/MicrosoftVirtualEarthSilverlightMapContr_C823/%E6%9C%AA%E5%91%BD%E5%90%8D3_2.jpg"></a>
第三步:在Silverlight的MainPage.xaml(2.0中是Page.xaml)中插入地圖控件。
<a></a>
<UserControl x:Class="SilverlightApplication1.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:Microsoft.VirtualEarth.MapControl;assembly=Microsoft.VirtualEarth.MapControl">
<Grid x:Name="LayoutRoot" Background="White">
<m:Map/>
</Grid>
</UserControl>
如果你F5運作它的話,你已經可以看到Virtual Earth地圖呈現在你的Silverlight項目裡了。我們還要做更多。
第四步:拖入一些簡單的控件。
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <UserControl x:Class="SLLiveEarthControl.MainPage"
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 xmlns:VirtualEarth="clr-namespace:Microsoft.VirtualEarth.MapControl;assembly=Microsoft.VirtualEarth.MapControl"
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 Width="700" Height="500">
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <Grid.RowDefinitions>
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <RowDefinition Height="*" />
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <RowDefinition Height="40" />
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 </Grid.RowDefinitions>
<VirtualEarth:Map x:Name="viewMap" Mode="AerialWithLabels" Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" Padding="5"
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 View="29.544,106.518,0.0000 14.0000 0.0000" MouseDoubleClick="MapWithImages_MouseDoubleClick" />
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <StackPanel Orientation="Horizontal" Opacity="0.7" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center">
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <Button x:Name="btnNorthAmerica" Click="ChangeMapView_Click" Tag="39.89341,116.33235,0.0000 14.0000 0.0000"
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 Margin="5" Height="20" >
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <TextBlock>北京</TextBlock>
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 </Button>
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <Button x:Name="btnChengdu" Click="ChangeMapView_Click" Tag="30.67,104.074,0.0000 14.0000 0.0000" Margin="5" Height="20" >
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <TextBlock>成都</TextBlock>
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <Button x:Name="btnShanghai" Click="ChangeMapView_Click" Tag="31.10,121.411,0.0000 13.0000 0.0000" Margin="5" Height="20" >
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <TextBlock>上海</TextBlock>
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <Button x:Name="btnVancouver" Click="ChangeMapView_Click" Tag="29.544,106.518,0.0000 14.0000 0.0000" Margin="5" Height="20" >
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <TextBlock>重慶</TextBlock>
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 </StackPanel>
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <StackPanel Orientation="Horizontal" Opacity="0.9" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Center">
<TextBlock Text="輕按兩下地圖可以添加圖釘。 更改經緯度後可點選Fly to換位置. " Padding="5" Foreground="Red" Width="178"TextWrapping="Wrap"/>
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <TextBlock Text="Latitude: " Padding="12" Foreground="Red" />
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <TextBox x:Name="txtLatitude" Text="" Height="20" />
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <TextBlock Text="Longitude: " Padding="12" Foreground="Red" />
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <TextBox x:Name="txtLongitude" Text="" Height="20" />
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <Button x:Name="btnFlyto" Click="btnFlyto_Click" Foreground="Red" Margin="5" Height="20" >
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 <TextBlock>Fly to</TextBlock>
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 第五步:寫背景代碼。
using System;
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 using System.Windows;
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 using System.Windows.Controls;
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 using System.Globalization;
using Microsoft.VirtualEarth.MapControl;
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 using Microsoft.VirtualEarth.MapControl.Design;
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 using System.Windows.Media;
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 using System.Windows.Media.Imaging;
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 namespace SLLiveEarthControl
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 {
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 public partial class MainPage : UserControl
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 {
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 LocationConverter locConverter = new LocationConverter();
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 // Converts the Button's location Tag to a map view..
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 MapViewSpecificationConverter viewConverter = new MapViewSpecificationConverter();
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 private MapLayer imageLayer = new MapLayer();
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 public MainPage()
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 {
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 InitializeComponent();
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 viewMap.AddChild(imageLayer);
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 viewMap.ViewChangeOnFrame += new EventHandler<MapEventArgs>(viewMap_ViewChangeOnFrame);
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 viewMap.AnimationLevel = AnimationLevel.Full;
}
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 private void viewMap_ViewChangeOnFrame(object sender, MapEventArgs e)
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 Map map = sender as Map;
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 if (map != null)
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 {
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 MapViewSpecification mapViewSpec = map.View;
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 // 根據地圖目前的狀态動态改變txtLatitude和txtLongitude的值
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 txtLatitude.Text = string.Format(CultureInfo.InvariantCulture,
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 "{0:F5}", mapViewSpec.Center.Latitude);
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 txtLongitude.Text = string.Format(CultureInfo.InvariantCulture,
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 "{0:F5}", mapViewSpec.Center.Longitude);
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 }
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 private void ChangeMapView_Click(object sender, RoutedEventArgs e)
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 viewMap.View = (MapViewSpecification)viewConverter.ConvertFrom(((Button)sender).Tag);
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 private void MapWithImages_MouseDoubleClick(object sender, MapMouseEventArgs e)
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 e.Handled = true;
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 Location pinLocation = viewMap.ViewportPointToLocation(e.ViewportPoint);
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 Image pin = new Image();
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 Random r = new Random();
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 pin.Source = new BitmapImage(new Uri(r.Next() % 2==0 ? "female.png" : "male.png", UriKind.RelativeOrAbsolute));
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 pin.Stretch = Stretch.None;
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 imageLayer.AddChild(pin, pinLocation, PositionMethod.BottomCenter);
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 private void btnFlyto_Click(object sender, RoutedEventArgs e)
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 viewMap.View = new MapViewSpecification(new Location(Double.Parse(txtLatitude.Text), Double.Parse(txtLongitude.Text)), 14);
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 }
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 }
Microsoft Virtual Earth Silverlight Map Control (CTP)快速上手 你可能已經注意到,我們在程式設計時已經完全脫離了Javascript;Virtual Earth在我們的手中,已經變得和TextBox,DataGrid一樣,隻是一個控制非常友善的控件了。 : )
本文轉自 流牛木馬 部落格園部落格,原文連結:http://www.cnblogs.com/azure/archive/2009/03/29/1424408.html,如需轉載請自行聯系原作者