2015-06-26 288 views
1

我有一个需求,我需要显示特定位置的图钉。 我已经能够添加一个自定义图钉,因为PushPin类现在已经被弃用了。 为其代码如下:Windows Phone应用程序8.1的图钉类似于Windows地图应用程序

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

我现在能够显示自定义脚却无法显示title.Is有办法我可以实现销如同在Windows地图应用程序中使用

我得到什么现在:

enter image description here

我想要什么:

enter image description here

事情是this.Or如果这是不可能添加标题销的方式。 请不要建议MapIcon因为我已经尝试过了,它不现身mapicon除非放大,还我发现它不太可靠

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

MapIcon的可选标题不保证显示。如果看不到文字,请通过减小MapControl的ZoomLevel属性的值来缩小。

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

回答

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

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

+0

这是好的,如果你想效仿旧形式的推针,但新的API与[MapIcons](https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn792121.aspx)似乎是一个更好的选择 – Petesh

+0

我尝试过使用这个代码MapIcon MapIcon1 = new MapIcon(); MapIcon1.Location = new Geopoint(startLocation); MapIcon1.NormalizedAnchorPoint = new Point(0.5,0.5); MapIcon1.Title =“太空针”; mapper.MapElements.Add(MapIcon1);然而,该图标不会购物,直到放大很多 – Pranjal

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

看朋友已经有一个现成的图钉类在名为“Microsoft.Phone.Maps”的名称空间中。工具包“,如果你不能将这个名字空间导入你的项目中,那么你需要添加它,你可以通过TOOLS> Library Package Manager> 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/

相关问题