public void AddMarkerGraphics()
{ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
GraphicsLayer graphicsLayer = myMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
//添加点信息
Graphic graphic = new Graphic()
{
Geometry = mercator.FromGeographic(new MapPoint(115.257113, 33.0696150000001)),
Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as Symbol
};
graphicsLayer.Graphics.Add(graphic);
//添加文字信息
TextSymbol textSymbol = new TextSymbol()
{
FontFamily = new System.Windows.Media.FontFamily("Microsoft YaHei"),
Foreground = new System.Windows.Media.SolidColorBrush(Color.FromArgb(255, 117, 20, 99)),
FontSize = 12,
Text = "需要添加的文字,可以根据需要动态读取赋值"
};
Graphic graphicText = new Graphic()
{
Geometry = mercator.FromGeographic(new MapPoint(115.257113, 33.0696150000001)),
Symbol = textSymbol
};
graphicsLayer.Graphics.Add(graphicText);
}
主要是利用TextSymbol类来作为文字的显示,然后添加到Graphics中去。
如果需要在动态添加图标记的同时,添加多个文字注视的话,比如在点的上方添加数值,点的下方添加名称,这样的话,我们可以调整的有TextSymbol的OffsetX和OffsetY属性,进行相应的调整即可达到实现目的。
#region 水位/雨量 数值
TextSymbol textSymbol2 = 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.YL24.ToString(),
OffsetX = 6,
OffsetY = 20
};
Graphic graphicText2 = 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 = textSymbol2
};
graphicText.Attributes["TextYL"] = item.YL24;
graphicsLayer.Graphics.Add(graphicText2);
#endregion
实际的效果如下图,并且随着地图的缩放,这些文字也是随着更改,不会出现位置偏差