ArcGIS API for Silverlight 之ElementLayer使用及TextSymbol的模板使用

來源:互聯網
上載者:User

在開發中動態在地圖上添加文字資訊,可以使用TextSymbol添加文字

//動態添加文本TextSymbol textSymbol = new TextSymbol(){        FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei"),        Foreground = new System.Windows.Media.SolidColorBrush(Color.FromArgb(255, 255, 0, 0)),        FontSize = 14,        Text = item.ZDMC,        OffsetX = 12,        OffsetY = -5};Graphic graphicText1 = new Graphic(){         Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim(), System.Globalization.CultureInfo.InvariantCulture), double.Parse(item.Longitute.ToString().Trim(), System.Globalization.CultureInfo.InvariantCulture))),         Symbol = textSymbol};graphicText1.Attributes["TextYLZMC"] = item.ZDMC;graphicsLayer1.Graphics.Add(graphicText1);

 

如果要考慮給文字加背景顏色的話,該類就無法起作用了

//動態添加帶背景顏色的文字資訊 Border b = new Border(); b.Background = new SolidColorBrush(Colors.Blue); b.Height = 20; b.CornerRadius = new CornerRadius(5); TextBlock tb = new TextBlock(); tb.Text = item.ZDMC; tb.Foreground = new SolidColorBrush(Colors.White); tb.HorizontalAlignment = HorizontalAlignment.Center; tb.FontSize = 15; b.Child = tb; ElementLayer.SetEnvelope(b, new Envelope(new MapPoint(double.Parse(item.Latitute.ToString().Trim(), System.Globalization.CultureInfo.InvariantCulture), double.Parse(item.Longitute.ToString().Trim(), System.Globalization.CultureInfo.InvariantCulture))),new MapPoint(double.Parse(item.Latitute.ToString().Trim(), System.Globalization.CultureInfo.InvariantCulture), double.Parse(item.Longitute.ToString().Trim(), System.Globalization.CultureInfo.InvariantCulture)))); elementLayer.Children.Add(b);

效果如下

 

提供第二種方法,使用TextSymbol的模板使用:

          <esri:TextSymbol x:Key="SWZTextSymbol">                <esri:TextSymbol.ControlTemplate>                    <ControlTemplate>                        <Border Background="White" BorderBrush="Black" CornerRadius="5"  BorderThickness="1,1,1,1">                            <TextBlock                                Width="70"                                FontSize="14"                                 Text="{Binding Attributes[TextSWZMC]}" //注意這裡的TextSWZMC,和後台代碼中要一致                                Foreground="Blue"                                FontFamily="Microsoft YaHei"                                HorizontalAlignment="Center" />                        </Border>                    </ControlTemplate>                </esri:TextSymbol.ControlTemplate>            </esri:TextSymbol>

 

  TextSymbol textSymbol = LayoutRoot.Resources["SWZTextSymbol"] as TextSymbol; //這裡和模板的Key名稱要一致  textSymbol.OffsetX = 15;  textSymbol.OffsetY = -15;  Graphic graphicText = new Graphic()   {         Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim(), System.Globalization.CultureInfo.InvariantCulture), double.Parse(item.Longitute.ToString().Trim(), System.Globalization.CultureInfo.InvariantCulture))),         Symbol = textSymbol   };   graphicText.Attributes["TextSWZMC"] = item.ZDMC; //注意這裡的TextSWZMC,和前台xaml代碼中要一致
   graphicsLayer11.Graphics.Add(graphicText);

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.