C# 适用于Windows Phone应用程序8.1的图钉,类似于Windows地图应用程序
我有一个要求,我需要显示一个特定位置的图钉。 由于pushpin类现在已被弃用,我可以添加一个自定义pushpin。 其代码如下:C# 适用于Windows Phone应用程序8.1的图钉,类似于Windows地图应用程序,c#,windows-phone-8.1,C#,Windows Phone 8.1,我有一个要求,我需要显示一个特定位置的图钉。 由于pushpin类现在已被弃用,我可以添加一个自定义pushpin。 其代码如下: var location= new Geopoint(new BasicGeoposition() { Latitude = 19.4034, Longitude = 73.8312 }); var pin= CreatePin(); mapper.Children.Add(pin); MapControl.SetLo
var location= new Geopoint(new BasicGeoposition() { Latitude = 19.4034, Longitude = 73.8312 });
var pin= CreatePin();
mapper.Children.Add(pin);
MapControl.SetLocation(pin, location);
MapControl.SetNormalizedAnchorPoint(pin, new Point(0.0, 1.0));
mapper.TrySetViewAsync(location, 16, 0, 0, MapAnimationKind.Bow);
自定义CreatePin的代码为:
private DependencyObject CreatePin()
{
//Creating a Grid element.
var myGrid = new Grid();
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.Background = new SolidColorBrush(Colors.Transparent);
//Creating a Rectangle
var myRectangle = new Rectangle {Fill = new SolidColorBrush(Colors.Black), Height = 20, Width = 20};
myRectangle.SetValue(Grid.RowProperty, 0);
myRectangle.SetValue(Grid.ColumnProperty, 0);
//Adding the Rectangle to the Grid
myGrid.Children.Add(myRectangle);
//Creating a Polygon
var myPolygon = new Polygon()
{
Points = new PointCollection() {new Point(2, 0), new Point(22, 0), new Point(2, 40)},
Stroke = new SolidColorBrush(Colors.Black),
Fill = new SolidColorBrush(Colors.Black)
};
myPolygon.SetValue(Grid.RowProperty, 1);
myPolygon.SetValue(Grid.ColumnProperty, 0);
//Adding the Polygon to the Grid
myGrid.Children.Add(myPolygon);
return myGrid;
}
我现在可以显示自定义pin,但无法显示标题。有没有办法实现windows地图应用中使用的pin
我现在得到的是:
我想要的是:
类似这样的东西。或者,如果这是一个不可能的方式添加标题到pin。
请不要建议使用MapIcon,因为我已经试过了,除非放大,否则它不会显示MapIcon,而且我发现它不太可靠
不保证显示MapIcon。当它遮挡地图上的其他元素或标签时,它可能会被隐藏
不保证显示MapIcon的可选标题。如果看不到文本,请通过减小MapControl的ZoomLevel属性的值缩小
当你在地图上显示一个指向一个特定位置的MaPIGIN图像时,例如,一个图钉或一个箭头——考虑将标准化的ActhoRooPoT属性的值设置为图像上指针的大致位置。如果将NormalizedAnchorPoint的值保留为其默认值(0,0),该值表示图像的左上角,则贴图缩放级别的更改可能会使图像指向其他位置
我可以将文本与多边形和矩形一起显示成一行。这是我能得到的最接近的结果
private DependencyObject CreatePin()
{
//Creating a Grid element.
var myGrid = new Grid();
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
myGrid.Background = new SolidColorBrush(Colors.Transparent);
//Creating a Rectangle
var myRectangle = new Rectangle {Fill = new SolidColorBrush(Colors.Blue), Height = 20, Width = 20};
myRectangle.SetValue(Grid.RowProperty, 0);
myRectangle.SetValue(Grid.ColumnProperty, 0);
//Adding the Rectangle to the Grid
myGrid.Children.Add(myRectangle);
//Creating a Polygon
var myPolygon = new Polygon()
{
Points = new PointCollection() {new Point(2, 0), new Point(22, 0), new Point(2, 40)},
Stroke = new SolidColorBrush(Colors.Blue),
Fill = new SolidColorBrush(Colors.Blue),
};
myPolygon.SetValue(Grid.RowProperty, 1);
myPolygon.SetValue(Grid.ColumnProperty, 0);
TextBlock newtextblock = new TextBlock();
newtextblock.Text = "hello";
newtextblock.FontSize = 15;
newtextblock.Foreground = new SolidColorBrush(Colors.Blue);
newtextblock.SetValue(Grid.RowProperty, 0);
newtextblock.SetValue(Grid.ColumnProperty, 1);
myGrid.Children.Add(newtextblock);
//Adding the Polygon to the Grid
myGrid.Children.Add(myPolygon);
return myGrid;
}
这看起来确实很难看。但这就是我所能想到的。如果有更好的方法实现这一点,请张贴答案。仍在等待更多答案。我能够将文本与多边形和矩形一起显示为一行。这是我能得到的最接近的结果
private DependencyObject CreatePin()
{
//Creating a Grid element.
var myGrid = new Grid();
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
myGrid.Background = new SolidColorBrush(Colors.Transparent);
//Creating a Rectangle
var myRectangle = new Rectangle {Fill = new SolidColorBrush(Colors.Blue), Height = 20, Width = 20};
myRectangle.SetValue(Grid.RowProperty, 0);
myRectangle.SetValue(Grid.ColumnProperty, 0);
//Adding the Rectangle to the Grid
myGrid.Children.Add(myRectangle);
//Creating a Polygon
var myPolygon = new Polygon()
{
Points = new PointCollection() {new Point(2, 0), new Point(22, 0), new Point(2, 40)},
Stroke = new SolidColorBrush(Colors.Blue),
Fill = new SolidColorBrush(Colors.Blue),
};
myPolygon.SetValue(Grid.RowProperty, 1);
myPolygon.SetValue(Grid.ColumnProperty, 0);
TextBlock newtextblock = new TextBlock();
newtextblock.Text = "hello";
newtextblock.FontSize = 15;
newtextblock.Foreground = new SolidColorBrush(Colors.Blue);
newtextblock.SetValue(Grid.RowProperty, 0);
newtextblock.SetValue(Grid.ColumnProperty, 1);
myGrid.Children.Add(newtextblock);
//Adding the Polygon to the Grid
myGrid.Children.Add(myPolygon);
return myGrid;
}
这看起来确实很难看。但这就是我所能想到的。如果有更好的方法实现这一点,请张贴答案。仍在等待更多答案。您可以创建样式,并将其作为如下样式提供:
<Style x:Key="PushPinFirmaStyle" TargetType="bm:Pushpin">
<Setter Property="Width" Value="51"/>
<Setter Property="Height" Value="54"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="DataContext" Value="{Binding }"/>
<Setter Property="Margin" Value="-26,-54,26,54"/>
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate>
<Grid x:Name="grdItem" DataContext="{Binding}" >
<Grid.Background>
<ImageBrush ImageSource="ms-appx:///Assets/icon/tesis.png" Stretch="Uniform"/>
</Grid.Background>
<TextBlock Text="{Binding NAME}" Margin="20,12,22,20 " Foreground="#003260"></TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Bing.Maps.Pushpin p=new Bing.Maps.Pushpin();
p.Style = (Style)Application.Current.Resources["PushPinFirmaStyle"];
您可以创建样式,并将其作为如下样式提供:
<Style x:Key="PushPinFirmaStyle" TargetType="bm:Pushpin">
<Setter Property="Width" Value="51"/>
<Setter Property="Height" Value="54"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="DataContext" Value="{Binding }"/>
<Setter Property="Margin" Value="-26,-54,26,54"/>
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate>
<Grid x:Name="grdItem" DataContext="{Binding}" >
<Grid.Background>
<ImageBrush ImageSource="ms-appx:///Assets/icon/tesis.png" Stretch="Uniform"/>
</Grid.Background>
<TextBlock Text="{Binding NAME}" Margin="20,12,22,20 " Foreground="#003260"></TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Bing.Maps.Pushpin p=new Bing.Maps.Pushpin();
p.Style = (Style)Application.Current.Resources["PushPinFirmaStyle"];
朋友,在名为“Microsoft.Phone.Maps.Toolkit”的命名空间中已经有一个现成的图钉类,如果无法将此命名空间导入到项目中,则需要添加它,可以通过转到工具>库包管理器>包管理器控制台,然后编写此命令“安装包WPtoolkit” 完成此操作后,需要在xaml代码中引用它:
xmlns:toolkit="clr-namespace:Microsoft.Phone.Maps.Toolkit;assembly=Microsoft.Phone.Controls.Toolkit"
然后您可以轻松地在C#中添加图钉:
结果应该是:
希望这有帮助:)
和平行动(Y)
参考资料:
朋友,在名为“Microsoft.Phone.Maps.Toolkit”的命名空间中已经有一个现成的图钉类,如果无法将此命名空间导入到项目中,则需要添加它,可以通过转到工具>库包管理器>包管理器控制台,然后编写此命令“安装包WPtoolkit” 完成此操作后,需要在xaml代码中引用它:
xmlns:toolkit="clr-namespace:Microsoft.Phone.Maps.Toolkit;assembly=Microsoft.Phone.Controls.Toolkit"
然后您可以轻松地在C#中添加图钉:
结果应该是:
希望这有帮助:)
和平行动(Y)
参考资料:
如果您尝试模拟旧形式的图钉,这是可以的,但是使用的新API似乎是一个更好的选择我尝试过使用以下代码的MapIcon MapIcon1=new MapIcon();MapIcon1.位置=新的地质点(位置);MapIcon1.NormalizedAnchorPoint=新点(0.5,0.5);MapIcon1.Title=“空间针”;mapper.MapElements.Add(MapIcon1);但是,图标在放大一段时间后才会显示。如果您尝试模拟旧形式的图钉,这是可以的,但是使用的新API似乎是一个更好的选择。我尝试过使用以下代码的MapIcon MapIcon1=new MapIcon();MapIcon1.位置=新的地质点(位置);MapIcon1.NormalizedAnchorPoint=新点(0.5,0.5);MapIcon1.Title=“空间针”;mapper.MapElements.Add(MapIcon1);然而,直到放大了很多图片,图标才会出现