微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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



   

在开发中动态在地图上添加文字信息,可以使用TextSymbol添加文字

  1. //动态添加文本  
  2. TextSymbol textSymbol = new TextSymbol()  
  3. {  
  4.         FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei"),  
  5.         Foreground = new System.Windows.Media.solidColorBrush(Color.FromArgb(255, 255, 0, 0)),  
  6.         FontSize = 14,  
  7.         Text = item.ZDMC,  
  8.         OffsetX = 12,  
  9.         OffsetY = -5  
  10. };  
  11.   
  12. Graphic graphicText1 = new Graphic()  
  13. {  
  14.          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))),  
  15.          Symbol = textSymbol  
  16. };  
  17.   
  18. graphicText1.Attributes["TextYLZMC"] = item.ZDMC;  
  19. graphicslayer1.Graphics.Add(graphicText1);  

 


如果要考虑给文字加背景颜色的话,该类就无法起作用了

  1. //动态添加带背景颜色的文字信息  
  2.  Border b = new Border();  
  3.  b.Background = new SolidColorBrush(Colors.Blue);  
  4.  b.Height = 20;  
  5.  b.CornerRadius = new CornerRadius(5);  
  6.  TextBlock tb = new TextBlock();  
  7.  tb.Text = item.ZDMC;  
  8.  tb.Foreground = new SolidColorBrush(Colors.White);  
  9.  tb.HorizontalAlignment = HorizontalAlignment.Center;  
  10.  tb.FontSize = 15;  
  11.  b.Child = tb;  
  12.   
  13.  ElementLayer.SetEnvelope(b, new Envelope(new MapPoint(double.Parse(item.Latitute.ToString().Trim(),new MapPoint(double.Parse(item.Latitute.ToString().Trim(), System.Globalization.CultureInfo.InvariantCulture))));  
  14.  elementLayer.Children.Add(b);  

效果如下

 

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

  1. <esri:TextSymbol x:Key="SWZTextSymbol">  
  2.       <esri:TextSymbol.ControlTemplate>  
  3.           <ControlTemplate>  
  4.               <Border Background="White" BorderBrush="Black" CornerRadius="5"  BorderThickness="1,1,1">  
  5.                   <TextBlock  
  6.                       Width="70"  
  7.                       FontSize="14"   
  8.                       Text="{Binding Attributes[TextSWZMC]}" //注意这里的TextSWZMC,和后台代码中要一致  
  9.                       Foreground="Blue"  
  10.                       FontFamily="Microsoft YaHei"  
  11.                       HorizontalAlignment="Center" />  
  12.               </Border>  
  13.           </ControlTemplate>  
  14.       </esri:TextSymbol.ControlTemplate>  
  15.   </esri:TextSymbol>  


 

  1. TextSymbol textSymbol = LayoutRoot.Resources["SWZTextSymbol"as TextSymbol; //这里和模板的Key名称要一致  
  2. textSymbol.OffsetX = 15;  
  3. textSymbol.OffsetY = -15;  
  4.   
  5. Graphic graphicText = new Graphic()  
  6.  {  
  7.        Geometry = mercator.FromGeographic(new MapPoint(double.Parse(item.Latitute.ToString().Trim(),  
  8.        Symbol = textSymbol  
  9.  };  
  10.  graphicText.Attributes["TextSWZMC"] = item.ZDMC; //注意这里的TextSWZMC,和前台xaml代码中要一致  
  1. graphicslayer11.Graphics.Add(graphicText);  
@H_343_502@ graphicslayer11.Graphics.Add(graphicText);


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐