教你Silverlight如何让BIND地图旋转

这里要说明的问题是对于SetView这个重载的方法中还有一个比较中要的属性就是Heading他表达了地图正北地图还是被旋转了。

对地图做旋转操 作要基于一个前提:对加载的地图源有特殊的要求,我用HttpWatch对可旋转地图的图源做了探测,发现跟普通的我们平常看到的图源不同,它有八种栅格 图片(0-7)如下图(我通过以上方法实现的旋转效果,但是地图源是必应可以鸟瞰的图源,普通的卫星地图不能达到这种效果),所以要实现地图的旋转操作要 考虑的这个问题,其实也不难理解我们普通用的地图源是3种栅格图片(0~3),必然不能进行操作。

关于微软必应地图(siverlight)的二次开发中,可能有的要对地图进行旋转操作,在其自身控件库中,就有一个可要操作的属性具体的XML代码如下:

<UserControl x:Class="SilverlightTest1.SwitchMapViews"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:m="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"
    Width="1024" Height="768">
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="40" />
      <RowDefinition Height="20" />
        </Grid.RowDefinitions>
        <m:Map x:Name="viewMap" CredentialsProvider="your key" Mode="AerialWithLabels"  Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" Padding="5"
               Center="39.3683,-95.2734,0.0000" ZoomLevel="4.000"/>
        <StackPanel rientation="Horizontal" pacity="0.7" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center">
            <Button x:Name="btnNorthAmerica" Click="ChangeMapView_Click" Tag="39.3683,-95.2734,0.0000 4.0000"
                     Margin="5">
                <TextBlock>North America</TextBlock>
            </Button>
            <Button x:Name="btnNewYork" Click="ChangeMapView_Click" Tag="40.7199,-74.0030,0.0000 12.0000" Margin="5">
                <TextBlock>New York</TextBlock>
            </Button>
            <Button x:Name="btnSanFrancisco" Click="ChangeMapView_Click" Tag="37.6801,-122.3395,0.0000 11.0000" Margin="5">
                <TextBlock>San Francisco</TextBlock>
            </Button>
            <Button x:Name="btnVancouver" Click="ChangeMapView_Click" Tag="49.2765,-123.1030,0.0000 14.0000" Margin="5">
                <TextBlock>Vancouver</TextBlock>
            </Button>
        </StackPanel>
        <StackPanel rientation="Horizontal" pacity="0.9" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Center">
            <TextBlock Text="Latitude: " Padding="5" Foreground="White"/>
            <TextBox x:Name="txtLatitude" Text="" IsReadOnly="True" Background="LightGray"/>
            <TextBlock Text="Longitude: " Padding="5" Foreground="White" />
            <TextBox x:Name="txtLongitude" Text="" IsReadOnly="True" Background="LightGray"/>
        </StackPanel>
    </Grid>
</UserControl>

后台代码如下:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Globalization;
using Microsoft.Maps.MapControl;
using Microsoft.Maps.MapControl.Design;

namespace SilverlightTest1
{
    public partial class SwitchMapViews : UserControl
    {
        LocationConverter locConverter = new LocationConverter();
       
        public SwitchMapViews()
        {
            InitializeComponent();

            // Displays the current latitude and longitude as the map animates.
            viewMap.ViewChangeOnFrame. += new EventHandler<MapEventArgs>(viewMap_ViewChangeOnFrame);
            // The default animation level: navigate between different map locations. viewMap.AnimationLevel = AnimationLevel.Full; } private void viewMap_ViewChangeOnFrame(object sender, MapEventArgs e)
        {
            // Gets the map object that raised this event.
            Map map = sender as Map;
            // Determine if we have a valid map object.
            if (map != null)
            {
                // Gets the center of the current map view for this particular frame.
                Location mapCenter = map.Center;

                // Updates the latitude and longitude values, in real time,
                // as the map animates to the new location.
                txtLatitude.Text = string.Format(CultureInfo.InvariantCulture,
                    "{0:F5}", mapCenter.Latitude);
                txtLongitude.Text = string.Format(CultureInfo.InvariantCulture,
                    "{0:F5}", mapCenter.Longitude);
            }
        }

        private void ChangeMapView_Click(object sender, RoutedEventArgs e)
        {
            // Parse the information of the button's Tag property
            string[] tagInfo = ((Button)sender).Tag.ToString().Split(' ');
            Location  center = (Location)locConverter.ConvertFrom(tagInfo[0]);
            double zoom = System.Convert.ToDouble(tagInfo[1]);
           
            // Set the map view
            viewMap.SetView(center, zoom);
        }
    }
}

共有0个回答