您的位置:首页 > 其它

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

2014-08-21 17:06 489 查看
哈哈,还是可以拖拽的图钉。我都快晕了。解决上一篇留下的问题。

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,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,
                Height = 23,
                ItemsSource = ctg
            };
            GV_cboxPopConType.SelectedIndex = 0;
            Button btnPopSaveConLonLat = new Button()
            {
                Content = "保存",
                Width = 75,
                Height = 23
            };
            btnPopSaveConLonLat.Click += btnPopSaveConLonLat_Click;
            Button btnPopCancel = new Button()
            {
                Content = "取消",
                Width = 75,
                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>

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



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



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



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



如果哪位大神有更好的方法,请联系。共同学习。谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐