接着上一篇,添加可以拖拽的图钉。这次采用超图本身的接口。Pushpin和InfoWindow类。
xaml代码:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<DataTemplate x:Key="LocationInfoWindowTemplate">
<StackPanel Width="312" Height="150">
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" Text="经度:" Grid.Row="0" Grid.Column="0"/>
<TextBlock VerticalAlignment="Center" Text="纬度:" Grid.Row="1" Grid.Column="0"/>
<TextBlock VerticalAlignment="Center" Text="类型:" Grid.Row="2" Grid.Column="0"/>
<TextBox Margin="0,5,0,5" x:Name="txtLongitudePopup" Text="老的经度" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" />
<TextBox Margin="0,5,0,5" x:Name="txtLatitudePopup" Text="老的纬度" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/>
<ComboBox x:Name="cboxPopConType" Margin="0,5,0,10" Grid.Row="2" Grid.Column="1">
<ComboBoxItem Tag="road" Content="道路" IsSelected="True"></ComboBoxItem>
<ComboBoxItem Tag="bridge" Content="桥梁"></ComboBoxItem>
<ComboBoxItem Tag="river" Content="河流"></ComboBoxItem>
<ComboBoxItem Tag="farmland" Content="农田"></ComboBoxItem>
</ComboBox>
<StackPanel HorizontalAlignment="Right" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal">
<Button x:Name="btnPopSaveConLonLat" Click="btnPopSaveConLonLat_Click_1" Content="保存" Width="70"/>
<Button x:Name="btnPopCancel" Click="btnPopCancel_Click_1" Content="取消" Width="70"/>
</StackPanel>
</Grid>
</StackPanel>
</DataTemplate>
</Grid.Resources>
<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();
public MainPage()
{
InitializeComponent();
}
private void btnDrag_Click_1(object sender, RoutedEventArgs e)
{
//获取数据模版中的控件 开始想给他赋值 但是没有实现 只可以读取。初步认为是他的原因:dataTemplate.LoadContent()
DataTemplate dataTemplate = LayoutRoot.Resources["LocationInfoWindowTemplate"] as System.Windows.DataTemplate;
Grid grid = (Grid)VisualTreeHelper.GetChild(dataTemplate.LoadContent(), 0);
ComboBox cbox = (ComboBox)grid.FindName("cboxPopConType");
TextBox txtLon = (TextBox)grid.FindName("txtLongitudePopup");
TextBox txtLat = (TextBox)grid.FindName("txtLatitudePopup");
txtLon.Text = "新的经度";
txtLat.Text = "新的纬度";</p><p> Drag_ElementsLayer.Children.Clear();
Pushpin p = new Pushpin();
InfoWindow window = new InfoWindow(MyMap);
window.ContentTemplate = LayoutRoot.Resources["LocationInfoWindowTemplate"] as System.Windows.DataTemplate;
window.Title = "合同地理位置信息";
bool isMouseCaptured = false;
p.MouseLeftButtonDown += (obj, ee) =>
{
isMouseCaptured = true;
MyMap.ScreenContainer.Children.Remove(window);
MyMap.ScreenContainer.Children.Add(window);
window.Location = p.Location;
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;
window.ShowInfoWindow();
};
p.Location = MyMap.Center;
p.Background = new SolidColorBrush(Colors.Blue);
Drag_ElementsLayer.AddChild(p);
MyMap.Layers.Add(Drag_ElementsLayer);
}
private void btnPopSaveConLonLat_Click_1(object sender, RoutedEventArgs e)
{
DataTemplate dataTemplate = LayoutRoot.Resources["LocationInfoWindowTemplate"] as System.Windows.DataTemplate;
Grid grid = (Grid)VisualTreeHelper.GetChild(dataTemplate.LoadContent(), 0);
ComboBox cbox = (ComboBox)grid.FindName("cboxPopConType");
ComboBoxItem item = cbox.SelectedItem as ComboBoxItem;
TextBox txtLon = (TextBox)grid.FindName("txtLongitudePopup");
TextBox txtLat = (TextBox)grid.FindName("txtLatitudePopup");</p><p> MessageBox.Show("经度:" + txtLon.Text + "\n" + "纬度:" + txtLat.Text + "\n" + "类型:" + item.Tag.ToString());
}</p><p> private void btnPopCancel_Click_1(object sender, RoutedEventArgs e)
{
MyMap.ScreenContainer.Children.Clear();
Drag_ElementsLayer.Children.Clear();
MyMap.Layers.Remove(Drag_ElementsLayer);
}
}
}
</p>
初始化截图,如下:
点击按钮添加图钉,左键图钉弹出交互窗口,接着拖动图钉。截图如下:
点击“取消”按钮删除图钉和图层。点击“保存”截图如下:
唉,可能您也发现了,想要得到数据模版的控件 并且给他赋值 但是没有实现 只可以读取。初步认为是他的原因:dataTemplate.LoadContent()。
如果有哪位大神可以做到,请留言。交流一下。刚开始学习,不熟练。谢谢。
源码下载:http://download.csdn.net/detail/duyelang/7797581