Silverlight4下bing map实现鼠标移动时显示地图上的经纬坐标

最近在学习Silverlight和bing地图的简单开发.只是做点简单的功能效果. 本人菜鸟入门级,大侠勿笑 微笑

环境和用到的工具如下

vs2010 

silverlight4 

Silverlight 4 toolkit April 2010

Bing Maps Silverlight Control


先贴一下界面的xaml代码.布局随便摆的,界面的布局后面还得多多研究


<UserControl
    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:shinyRed="clr-namespace:System.Windows.Controls.Theming;assembly=System.Windows.Controls.Theming.ShinyRed"
    xmlns:System_Windows_Controls_Primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:ee="http://schemas.microsoft.com/expression/2010/effects" x:Class="SilverlightStudy1.MainPage"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" MinWidth="1024" MinHeight="600" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" Loaded="UserControl_Loaded" xmlns:my="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl">

    <toolkit:DockPanel Name="RootLayer" Width="auto" Height="auto">
        <StackPanel x:Name="LayoutTop" Orientation="Vertical" Height="100" VerticalAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Auto" toolkit:DockPanel.Dock="Top">
            <StackPanel.Background>
                <ImageBrush ImageSource="/SilverlightStudy1;component/Images/top_pic.png" />
            </StackPanel.Background>
        </StackPanel>
        <Grid Name="Center" Width="auto" ShowGridLines="False" Margin="0,2,0,0" toolkit:DockPanel.Dock="Bottom">
            <!-- 定义表格结构 -->
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition Width="auto"></ColumnDefinition>
                <ColumnDefinition Width="3*" MinWidth="500"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <Rectangle Grid.Row="0" Grid.Column="0" Name="leftPanel" Stroke="#FF031011"></Rectangle>
            <Rectangle Grid.Row="0" Grid.Column="2" Name="rightPanel" Stroke="#FF140902"></Rectangle>

            <sdk:GridSplitter x:Name="splitLine" Grid.Column="1" HorizontalAlignment="Center" Width="6" BorderThickness="1" BorderBrush="#FF0A0A02" />
            <!-- 以下存放内容控件 -->
            <shinyRed:ShinyRedTheme Grid.Column="3" Height="auto">
                <sdk:TabControl x:Name="Tab" Width="auto" Grid.Column="2" Height="auto" DataContext="{Binding RelativeSource={RelativeSource Self}}">
                    <sdk:TabItem Content="这里是首页的内容" Header="首 页" BorderThickness="0" Foreground="White" />
                    <sdk:TabItem Content="内容管理页面" Header="内容管理" BorderThickness="0" Foreground="White" />
                    <sdk:TabItem Header="地 图" Foreground="White">
                        <Grid x:Name="MapCanvas" Width="auto" Height="auto">
                            <Grid.RowDefinitions>
                                <RowDefinition></RowDefinition>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*">
                                </ColumnDefinition>
                                <ColumnDefinition Width="auto"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            
                            <my:Map x:Name="mapone" CredentialsProvider="An3GS6Xxqc0vv9sOqv1xpY3bTSKMHsfgMJobV7qgipzukAHWXKIvUtp25OHNX8aG" Center="30.280,114.500" ZoomLevel="9" Width="auto" Height="auto" Grid.Row="0" Grid.Column="0" Mode="Road" MouseMove="mapone_MouseMove" DataContext="{Binding Path=Resources.Source.Scheme}">
                                <!--<my:Map.Children>-->
                                    <!-- 在xmal中直接标点 -->
                                    <!--<my:Pushpin Location="30.111,114.6945"></my:Pushpin>-->
                                    
                                    <!-- 在xmal中直接画线 -->
                                    <!--<my:MapPolyline Stroke="Red" StrokeThickness="1" Locations="28.111,114.2232 29.2,114.899 30.12,117.888">    
                                    </my:MapPolyline>-->
                                <!--</my:Map.Children>-->
                            </my:Map>
                            <StackPanel x:Name="mapOperationPanel" Orientation="Vertical" Width="100" Height="auto" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right">
                                <Button x:Name="btnEnlage" Width="80" Height="24" Content="放大" Margin="0,10,0,0"></Button>
                                <Button x:Name="btnDeflation" Width="80" Height="24" Content="缩小" Margin="0,10,0,0"></Button>
                                <sdk:Label x:Name="labTooltip" Content="地图显示模式:" Foreground="Red" Margin="5,10,0,0" />
                                <ComboBox x:Name="ddlMapMode" Width="90" Margin="0,5,0,0" SelectionChanged="ddlMapMode_SelectionChanged"></ComboBox>
                                <sdk:Label Content="纬度:" Margin="10,25,0,0"></sdk:Label>
                                <TextBox x:Name="txtLat" Width="90" Height="23"></TextBox>
                                <sdk:Label Content="经度:" Margin="10,3,0,0"></sdk:Label>
                                <TextBox x:Name="txtLong" Width="90" Height="23"></TextBox>
                                <Button x:Name="btnPosition" Content="定位" Width="80" Height="23" Margin="0,5,0,0" Click="btnPosition_Click"></Button>
                                <Button x:Name="btnRegMouseClick" Content="开启标记功能" Width="100" Height="23" Margin="0,20,0,0" Click="btnRegMouseClick_Click"></Button>
                                <Button x:Name="btnCleanMouseClick" Content="结束标记功能" Width="100" Height="23" Margin="0,5,0,0" Click="btnCleanMouseClick_Click"></Button>
                            </StackPanel>
                            
                            <!--- 右键菜单 -->
                            <toolkit:ContextMenuService.ContextMenu>
                                <toolkit:ContextMenu Name="RightKeyMenu">
                                    <toolkit:MenuItem Header="开启标记功能"></toolkit:MenuItem>
                                    <toolkit:MenuItem Header="结束标记功能"></toolkit:MenuItem>
                                    <toolkit:Separator></toolkit:Separator>
                                    <toolkit:MenuItem Header="放大"></toolkit:MenuItem>
                                    <toolkit:MenuItem Header="缩小"></toolkit:MenuItem>
                                    <!--MenuItem详细定义格式-->
                                    <toolkit:MenuItem x:Name="testStyleItem">
                                        <toolkit:MenuItem.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <!--定义选项前面的小图标-->
                                                <Image x:Name="itemIco1" Width="28" Height="28" Source="/SilverlightStudy1;component/Images/fj.jpg"></Image>
                                                <TextBlock Text="测试选项" Padding="5"></TextBlock>
                                            </StackPanel>
                                        </toolkit:MenuItem.Header>
                                    </toolkit:MenuItem>
                                </toolkit:ContextMenu>
                            </toolkit:ContextMenuService.ContextMenu>
                        </Grid>                        
                    </sdk:TabItem>
                </sdk:TabControl>
            </shinyRed:ShinyRedTheme>
        </Grid>
    </toolkit:DockPanel>
</UserControl>


小功能一:要求在鼠标在地图上滑动时,显示出鼠标在地图上所处位置的经纬度坐标.

我们先在Bing Map控件的元素<Map> 里面添加一个鼠标滑动的事件 .如上代码中,定的事件MouseMove="mapone_MouseMove"   注意:我这里给<Map>元素定了个Name,后面要用到的 Name="mapone"

事件定好了,我们还要在界面上给一个或两个控件用于显示经纬坐标信息. 我这里是在后台代码中动态添加的几个Label控件.大伙可以自己去工具箱里面拖出来放到界面上.

那现在我们来看看后台代码:

        /// <summary>
        /// 鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mapone_MouseMove(object sender, MouseEventArgs e)
        {
            //获取动态添加的两个Label控件
            Label latValue = this.mapOperationPanel.FindName("latX") as Label;
            Label longValue = this.mapOperationPanel.FindName("longY") as Label;


            //根据鼠标在控件(地图控件mapone)的布局位置来获取鼠标在地图中的位置
            Location mapLocation = this.mapone.ViewportPointToLocation(e.GetPosition(this.mapone));


            //把得到的经纬度坐标信息显示出来.
            latValue.Content = mapLocation.Latitude.ToString();
            longValue.Content = mapLocation.Longitude.ToString();
        }

就一句代码的事. 最主要的还是Bing Map控件自带的一个方法 ViewportPointToLocation() 这个方法是接收一个Point类型的参数,这个方法就是根据视口点坐标获取相应的地图坐标;


小功能二:



额... 贴了一大堆乱码,其实就是这么个简单的问题.  不过我个人还是觉得,在这里面写点这样的东东可以让我学得更好.至少写这篇废话前,这问题虽然是实现了,但是还有点小问题,自己绕了点弯子.写的时候就发现代码写的太废话了.



©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页