我正在使用Visual Studio Community 2017。
我所拥有的和有效的方法:
它看起来像这样:
我现在要做什么:从两个屏幕截图中可以看到,网格的大小是静态的,并且在用户放大时不会改变。我想完全更改:我希望网格具有固定到地图的大小,即当用户放大/缩小时,网格大小会发生变化。具体来说,我想:
它看起来(大致)如下图所示。红色实心圆圈突出显示该点(47.6424,-122.3219)。当用户缩小时,网格应根据Bing地图的比例动态变小。两个图中的网格缩放比例并非100%准确,但是您可以理解。
我如何做到这一点? 注意:以后,我将需要做同样的事情,但要使用六角形网格。我提到这一点是为了防止这种情况改变我想采取的方法。
谢谢!
XAML:(您需要Bing地图的密钥)
<Window x:Class="squareGridBing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ProofOfConcept"
mc:Ignorable="d"
Title="Square grid on Bing Map" Height="450" Width="808.4">
<Grid>
<m:Map Margin="5,0,249.6,0" CredentialsProvider="[YOUR KEY TO BE INSERTED HERE]">
<Grid ShowGridLines="True" Margin="100,100,220,100" Opacity="0.5">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" Click="ButtonClick" />
<Button Grid.Column="1" Grid.Row="0" Click="ButtonClick" />
<Button Grid.Column="2" Grid.Row="0" Click="ButtonClick" />
<Button Grid.Column="0" Grid.Row="1" Click="ButtonClick" />
<Button Grid.Column="1" Grid.Row="1" Click="ButtonClick" />
<Button Grid.Column="2" Grid.Row="1" Click="ButtonClick" />
<Button Grid.Column="0" Grid.Row="2" Click="ButtonClick" />
<Button Grid.Column="1" Grid.Row="2" Click="ButtonClick" />
<Button Grid.Column="2" Grid.Row="2" Click="ButtonClick" />
</Grid>
</m:Map>
</Grid>
CS:
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace squareGridBing
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonClick(object sender, RoutedEventArgs e)
{
Button _btn = sender as Button;
int _row = (int)_btn.GetValue(Grid.RowProperty);
int _column = (int)_btn.GetValue(Grid.ColumnProperty);
MessageBox.Show(string.Format("Button clicked at column {0}, row {1}", _column, _row));
}
}
}
答案 0 :(得分:1)
您是否考虑过将栅格(或六边形,因为您想将其扩展到其他形状)作为多边形直接添加到地图? https://msdn.microsoft.com/en-us/library/hh868034.aspx 以这种方式添加到地图的多边形会随地图内容移动和缩放。当用户单击事件以获取其单击的单元格时,您应该能够收听事件。