2015-06-26 173 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/

相關問題