适用于Windows Phone App 8.1的图钉类似于Windows Map App

时间:2015-06-26 13:17:03

标签: c# windows-phone-8.1

我有一个要求,我需要显示特定位置的图钉。 我可以添加自定义图钉,因为现在不推荐使用PushPin类。 代码如下:

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;
    }

我现在可以显示自定义引脚但无法显示标题。有没有办法可以实现像windows地图应用中使用的引脚

我现在得到了什么:

enter image description here

我想要的是什么:

enter image description here

像这样的东西。如果这不可能是一种向pin添加标题的方法。 请不要建议MapIcon,因为我已经尝试了它并没有显示mapicon除非放大,我发现它不太可靠

  

无法保证显示MapIcon。当它遮挡地图上的其他元素或标签时,它可能会被隐藏。

     

无法保证显示MapIcon的可选标题。如果您没有看到文本,请通过减小MapControl的ZoomLevel属性值来缩小。

     

当您显示指向地图上特定位置的MapIcon图像时(例如,图钉或箭头),请考虑将NormalizedAnchorPoint属性的值设置为图像上指针的大致位置。如果将NormalizedAnchorPoint的值保留为其默认值(0,0)(表示图像的左上角),则地图的ZoomLevel中的更改可能会使图像指向不同的位置。

3 个答案:

答案 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;
    }

enter image description here

这看起来非常难看。但这是我能想到的。如果有更好的方法来实现这一点,请发布答案。还在等待更多答案。

答案 1 :(得分:0)

你可以创建样式,你可以把它作为这样的样式:

 <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"];

答案 2 :(得分:0)

看看朋友已经在名为“Microsoft.Phone.Maps.Toolkit”的命名空间中有一个现成的PushPin类,如果你不能将这个命名空间导入你的项目然后你需要添加它,你可以通过去到TOOLS&gt; Library Package Manager&gt; Package Manager Console,然后编写此命令“Install-Package WPtoolkit”

enter image description here

完成此操作后,您需要在xaml代码中引用它:

xmlns:toolkit="clr-namespace:Microsoft.Phone.Maps.Toolkit;assembly=Microsoft.Phone.Controls.Toolkit"

然后您可以轻松地在C#中添加图钉:

using Microsoft.Phone.Controls;
using System.Device.Location;
using Windows.Devices.Geolocation;
using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Maps.Toolkit;
Pushpin pin = new Pushpin()
        {
            Content = "MyPushpin",
            Background = new SolidColorBrush(Colors.Blue),
            BorderBrush = new SolidColorBrush(Colors.White)
        };
MapOverlay relay = new MapOverlay()
        {
            Content = pin,
            PositionOrigin = new Point(0.5, 0.5),
            GeoCoordinate = myGeoCoordinates // myGeoCoordinates is an object of the class GeoCoordinate
        };
MapLayer layer = new MapLayer();
layer.Add(relay);
myMap.Layers.Add(layer); // myMap is the name of your map control 

这应该是结果:

enter image description here

希望这有助于:)

PeaceBeUponYou(Y)

  
    

参考文献:

  
     

http://www.codeproject.com/Tips/806054/Windows-Phone-Location-Map-Pushpin   http://phone.codeplex.com/