supermap学习系列之silverlight--添加可拖拽的定位图钉(方法一)

学习笔记。

具体的怎么配置silverlight版本的地图,可以看他的官方帮助文档。

xaml代码:

<Grid x:Name="LayoutRoot" Background="White">     
        <ic:Map x:Name="MyMap">
            <!--地图地址  自己添加-->
            <is:TiledDynamicRESTLayer Url="" />  
        </ic:Map>
        <Popup x:Name="dragMarkerPopup">
            <StackPanel Width="312" Height="220">
                <StackPanel.Background>
                        <!--<ImageBrush ImageSource="/images/infoWindow.png" Stretch="Fill"/>-->
                    <ImageBrush ImageSource="/images/1.png" Stretch="Fill"/>
                </StackPanel.Background>
                <Grid Margin="5">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="40" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="合同地理位置信息" Grid.Row="0" Grid.ColumnSpan="2" Grid.Column="0"/>
                    <TextBlock VerticalAlignment="Center" Text="经度:" Grid.Row="1" Grid.Column="0"/>
                    <TextBlock VerticalAlignment="Center" Text="纬度:" Grid.Row="2" Grid.Column="0"/>
                    <TextBlock VerticalAlignment="Center" Text="类型:" Grid.Row="3" Grid.Column="0"/>
                    <TextBox Margin="0,5,0,5" x:Name="txtLongitudePopup" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" />
                    <TextBox Margin="0,5,0,5" x:Name="txtLatitudePopup" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"/>
                    <ComboBox Margin="0,5,0,10" Grid.Row="3" Grid.Column="1"/>
                    <StackPanel HorizontalAlignment="Right" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal">
                        <Button x:Name="btnPopSaveConLonLat" Content="保存" Width="70"/>
                        <Button x:Name="btnPopCancel" Click="btnPopCancel_Click_1" Content="取消" Width="70"/>
                    </StackPanel>
                </Grid>
            </StackPanel>        
        </Popup>
        <Button x:Name="btnDrag" Content="点击" Height="23" Width="75" Click="btnDrag_Click_1" Margin="0,0,925,277" />
    </Grid>

后台代码:

<p> public partial class MainPage : UserControl
    {
        private FeaturesLayer Drag_FeaturesLayer = new FeaturesLayer();
        public MainPage()
        {
            InitializeComponent();
        }           
           
        private void btnDrag_Click_1(object sender, RoutedEventArgs e)
        {
            Feature f = new Feature();
            f.Geometry = new GeoPoint(MyMap.Center.X, MyMap.Center.Y);
            PictureMarkerStyle picMarker = new PictureMarkerStyle();
            picMarker.Source = new BitmapImage(new Uri("/images/6.png", UriKind.Relative));</p><p>            picMarker.Height = 37;
            picMarker.Width = 32;
            f.Style = picMarker;
            Drag_FeaturesLayer.Features.Add(f);
            MyMap.Layers.Add(Drag_FeaturesLayer);</p><p>            bool isMouseCaptured = false;
            f.MouseLeftButtonDown += (sender_, args) =>
            {
                isMouseCaptured = true;
                dragMarkerPopup.VerticalOffset = args.GetPosition(null).Y-210;
                dragMarkerPopup.HorizontalOffset = args.GetPosition(null).X-70;
                dragMarkerPopup.IsOpen = true;
            };
            f.MouseMove += (sender_, args) =>
            {
                if (isMouseCaptured)
                {                  
                    MyMap.Action = null;
                    Point2D new_point2d = MyMap.ScreenToMap(new Point(args.GetPosition(null).X, args.GetPosition(null).Y));
                    f.Geometry = new GeoPoint(new_point2d.X, new_point2d.Y);
                    dragMarkerPopup.VerticalOffset = args.GetPosition(null).Y-210;
                    dragMarkerPopup.HorizontalOffset = args.GetPosition(null).X-60;
                    txtLongitudePopup.Text = new_point2d.X.ToString();
                    txtLatitudePopup.Text = new_point2d.Y.ToString();
                }
            };
            f.MouseLeftButtonUp += (sender_, args) =>
            {
                isMouseCaptured = false;
                Point2D new_point2d = MyMap.ScreenToMap(new Point(args.GetPosition(null).X, args.GetPosition(null).Y));              
                MyMap.Action = new Pan(MyMap);
                txtLongitudePopup.Text = new_point2d.X.ToString();
                txtLatitudePopup.Text = new_point2d.Y.ToString();
            };
        }</p><p>        private void btnPopCancel_Click_1(object sender, RoutedEventArgs e)
        {
            dragMarkerPopup.IsOpen = false;
            Drag_FeaturesLayer.Features.Clear();
            MyMap.Layers.Remove(Drag_FeaturesLayer);
        }     
    }</p>

初始化截图如下:



添加页面中的button,添加图钉,点击图钉左键可以拖拽,截图如下:

图钉和Popup窗口一起动。点击取消按钮关闭Popup和删除图钉。

源码下载:http://download.csdn.net/detail/duyelang/7797059

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值