2017-02-22 54 views
0

我一直在研究互聯網上的各種方法來更改Bing Maps C#控件中的圖釘圖像。C#中的Bing地圖 - 變化圖釘圖片

Change image for pushpin WPF

這並不完全是我要找的,因爲我希望能夠改變推針的顏色,以及添加一個:我用的是下面的解決方案來最接近標籤。

上述解決方案基本上是通過推針繪製的圖像,不需要額外的功能,如添加標籤。我希望能夠在定製標籤功能的同時輕鬆更改圖像。

有沒有其他方式做到這一點?另一種方法是使用「標準」Bing推針圖形並能夠改變尺寸。但是看來這個功能是不是在C#控制

+0

[爲圖釘WPF更改圖片]的可能的複製(http://stackoverflow.com/questions/14559630/change-image-for-pushpin-wpf) –

+0

顯示你已經嘗試過,並爭辯爲什麼這個其他的解決方案不適合你。如果包含[mcve],則總是可取的。現在,這個問題是重複的,可能會被關閉。 –

+0

https://openlayers.org/對於抽象層和許多示例總是很好,你想要做什麼 – lordkain

回答

0

令人驚訝的,這是關於這個主題的SOF唯一的問題(在other one關閉)。要在WPF中完成新手(和我一樣)很難找到好的並且同時有簡單的信息,所以我將展示如何在VB.NET中以編程方式添加圖釘時使用自定義圖像:

這是我的MainWindow.xaml文件:

<Window x:Class="MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF" 
     xmlns:local="clr-namespace:MyBingMapsApp" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <ControlTemplate x:Key="PushpinControlTemplate" TargetType="m:Pushpin"> 
      <Grid> 
       <Rectangle Width="24" Height="24"> 
        <Rectangle.Fill> 
         <ImageBrush ImageSource= "Resources/marker_green.png"/> 
        </Rectangle.Fill> 
       </Rectangle> 
      </Grid> 
     </ControlTemplate> 
    </Window.Resources> 
    <Grid> 
     <m:Map x:Name="myMap" 
       CredentialsProvider= "xxxxxx mycredentialskey xxxxxxxx" 
       Center="42.13618,-0.40822" 
       ZoomLevel="15"> 
     </m:Map> 
    </Grid> 
</Window> 

正如你所看到的,你必須定義一個ControlTemplateTargetType="m:Pushpin" 在那裏,你可以畫任何你所需要的。最簡單的方法是:使用資源中的圖片。

重要:改變形象「建設行動」 資源(在資源解決方案管理器的文件夾,點擊圖片,並改變它在高級設置)。否則,你將不得不hardwrite圖像路徑或使用URI或more complicated stuff

現在,您可以創建一個圖釘你需要的地方,並指定你的模板:

mypin = New Pushpin 
mypin.Location = New Location(mylat, mylon) 
mypin.ToolTip = "This is a pushpin with custom image" 

Dim mytemplate As System.Windows.Controls.ControlTemplate = FindResource(「PushpinControlTemplate」) 'here of course you must put the key name of your template 
mypin.Template = mytemplate