微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

supermap学习系列之silverlight--添加可拖拽的定位图钉方法二之超图自带类Pushpin、InfoWindow 续 解决上一篇的问题

哈哈,还是可以拖拽的图钉。我都快晕了。解决上一篇留下的问题。

xaml代码

 <Grid x:Name="LayoutRoot" Background="White">     
        <ic:Map x:Name="MyMap">
            <!--地图地址   自己填写-->
            <is:TiledDynamicRESTLayer Url="<a target=_blank href="http://localhost:8090/iserver/services/map-china400/rest/maps/China">http://localhost:8090/iserver/services/map-china400/rest/maps/China</a>" />  
        </ic:Map>      
        <Button x:Name="btnDrag" Content="点击" Height="23" Width="75" Click="btnDrag_Click_1" Margin="0,925,277" />
    </Grid>

cs后台代码

<p>namespace SL
{
    public partial class MainPage : UserControl
    {
        private ElementsLayer Drag_ElementsLayer = new ElementsLayer();
        private ComboBox GV_cBoxPopConType;
        private TextBox GV_txtLon;
        private TextBox GV_txtLat;
        public MainPage()
        {
            InitializeComponent();
        }           
           
        private void btnDrag_Click_1(object sender,RoutedEventArgs e)
        {            
            Drag_ElementsLayer.Children.Clear();
            pushpin p = new pushpin();
            InfoWindow window = GetPopupContent();
           
            bool isMouseCaptured = false;
            p.MouseLeftButtonDown += (obj,ee) =>
            {
                isMouseCaptured = true;
                MyMap.ScreenContainer.Children.Remove(window);
                MyMap.ScreenContainer.Children.Add(window);
                window.Location = p.Location;
                if (GV_txtLon != null)
                {
                    GV_txtLon.Text = p.Location.X.ToString();
                }
                if (GV_txtLat != null)
                {
                    GV_txtLat.Text = p.Location.Y.ToString();
                }
                window.ShowInfoWindow();
            };
            p.MouseMove += (obj,ee) =>
            {
                if (isMouseCaptured)
                {
                    MyMap.Action = null;
                    Point2D new_point2d = MyMap.ScreenToMap(new Point(ee.GetPosition(null).X,ee.GetPosition(null).Y+37));
                    p.Location = new_point2d;
                    window.CloseInfoWindow();
                }
            };
            p.MouseLeftButtonUp += (obj,ee) =>
            {
                isMouseCaptured = false;
                MyMap.Action = new Pan(MyMap);
                Point2D new_point2d = MyMap.ScreenToMap(new Point(ee.GetPosition(null).X,ee.GetPosition(null).Y+37));
                p.Location = new_point2d;
                window.Location = p.Location;
                if (GV_txtLon != null)
                {
                    GV_txtLon.Text = p.Location.X.ToString();
                }
                if (GV_txtLat != null)
                {
                    GV_txtLat.Text = p.Location.Y.ToString();
                }
                window.ShowInfoWindow();
            };
            p.Location = MyMap.Center;
            p.Background = new SolidColorBrush(Colors.Blue);
            Drag_ElementsLayer.AddChild(p);
            MyMap.Layers.Add(Drag_ElementsLayer);
        }</p><p>        private InfoWindow GetPopupContent()
        {
            StackPanel spAll = new StackPanel();
            StackPanel sp1 = new StackPanel()
            {
                Orientation = Orientation.Horizontal,                Height = 29
            };
            StackPanel sp2 = new StackPanel()
            {
                Orientation = Orientation.Horizontal,                Height = 29
            };
            StackPanel sp3 = new StackPanel()
            {
                Orientation = Orientation.Horizontal,                Height = 29
            };
            StackPanel sp4 = new StackPanel()
            {
                Orientation = Orientation.Horizontal,                Height = 29,                HorizontalAlignment = HorizontalAlignment.Right
            };
            TextBlock tblockLon = new TextBlock()
            {
                Text = "经度:",                VerticalAlignment = VerticalAlignment.Center
            };
            TextBlock tblockLat = new TextBlock()
            {
                Text = "纬度:",                VerticalAlignment = VerticalAlignment.Center
            };
            TextBlock tblockType = new TextBlock()
            {
                Text = "类型:",                VerticalAlignment = VerticalAlignment.Center
            };
            GV_txtLon = new TextBox()
            {
                Width = 256,                Height = 23,            };
            GV_txtLat = new TextBox()
            {
                Width = 256,                Height = 23
            };
            List<ComboBoxItem> ctg = new List<ComboBoxItem>()
            {
                new ComboBoxItem { Tag="road",Content="道路" },                new ComboBoxItem { Tag="bridge",Content="桥梁" },                new ComboBoxItem { Tag="river",Content="河流" },                new ComboBoxItem { Tag="farmland",Content="农田" },              
            };
            GV_cBoxPopConType = new ComboBox()
            {
                Width = 256,                ItemsSource = ctg
            };
            GV_cBoxPopConType.Selectedindex = 0;
            Button btnPopSaveConLonLat = new Button()
            {
                Content = "保存",                Width = 75,                Height = 23
            };
            btnPopSaveConLonLat.Click += btnPopSaveConLonLat_Click;
            Button btnPopCancel = new Button()
            {
                Content = "取消",                Height = 23
            };
            btnPopCancel.Click += btnPopCancel_Click;
            sp1.Children.Add(tblockLon);
            sp1.Children.Add(GV_txtLon);
            sp2.Children.Add(tblockLat);
            sp2.Children.Add(GV_txtLat);</p><p>            sp3.Children.Add(tblockType);
            sp3.Children.Add(GV_cBoxPopConType);</p><p>            sp4.Children.Add(btnPopSaveConLonLat);
            sp4.Children.Add(btnPopCancel);</p><p>            spAll.Children.Add(sp1);
            spAll.Children.Add(sp2);
            spAll.Children.Add(sp3);
            spAll.Children.Add(sp4);
         
            InfoWindow window = new InfoWindow(MyMap);
            window.Width = 310;
            window.Height = 150;
            window.Content = spAll;
            window.Title = "地理位置信息";
            return window;
        }</p><p>        private void btnPopCancel_Click(object sender,RoutedEventArgs e)
        {
            MyMap.ScreenContainer.Children.Clear();
            Drag_ElementsLayer.Children.Clear();
            MyMap.Layers.Remove(Drag_ElementsLayer);
        }</p><p>        private void btnPopSaveConLonLat_Click(object sender,RoutedEventArgs e)
        {
            string type = "",lon = "",lat = "";
            if (GV_cBoxPopConType != null)
            {
                ComboBoxItem item = GV_cBoxPopConType.SelectedItem as ComboBoxItem;
                type = item.Tag.ToString();
            }
            if (GV_txtLon != null)
            {
                lon = GV_txtLon.Text.Trim();
            }
            if (GV_txtLat != null)
            {
                lat = GV_txtLat.Text.Trim();
            }
            MessageBox.Show("经度:" + lon + "\n" + "纬度:" + lat + "\n" + "类型:" + type);
        }         
    }
}</p>

初始化截图和上一篇是一样的,如下:


点击按钮添加图钉,左键弹出交互窗口等和上一篇一样。不一样的是保存,截图如下:

点击保存按钮,截图如下:

 

切换图钉位置以及下拉框选项,可以和上一张对比一下,截图如下:

 

如果哪位大神有更好的方法,请联系。共同学习。谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐