接着上一节继续学习,本节主要是利用ElementLayer实现鼠标悬浮弹出自定义窗体
参考博文:http://www.cnblogs.com/luxiaoxun/p/3322218.html
一、新建silverlight用户控件
自定义窗体,可以放置很多控件,命名为MyMapTip.xaml,代码为:
<Grid x:Name="LayoutRoot" Width="400" Height="100" Background="Transparent">
<Border CornerRadius="5">
<Border.Background>
<SolidColorBrush Color="Black" Opacity="0.5"/>
</Border.Background>
<StackPanel Margin="5" >
<TextBlock Foreground="Wheat" TextWrapping="Wrap" Name="textHEADLINE" ></TextBlock>
<TextBlock Foreground="Wheat" TextWrapping="Wrap" Name="textEFFECTIVE" ></TextBlock>
<TextBlock Foreground="Wheat" TextWrapping="Wrap" Name="textDESCRIPTION" ></TextBlock>
</StackPanel>
</Border>
</Grid>
后台代码为:
public MyMapTip(string EFFECTIVE, string HEADLINE, string DESCRIPTION)
{
InitializeComponent();
textEFFECTIVE.Text = "发布时间:" + EFFECTIVE;
textHEADLINE.Text = "标题:" + HEADLINE;
textDESCRIPTION.Text = DESCRIPTION;
}
二、实现自定义窗体弹出
1、定义全局变量:ElementLayer elementLayer = new ESRI.ArcGIS.Client.ElementLayer();
2、在地图中加载ElementLayer(最好放在加载GraphicsLayer之后,图层加载顺序:后加载的在上层):
this.myMap.Layers.Add(elementLayer);
3、为Graphic添加属性,并为GraphicsLayer增加鼠标事件
private void AddLightMarker()
{
GraphicsLayer glayer = new ESRI.ArcGIS.Client.GraphicsLayer();
Graphic g = new Graphic()
{
Geometry = mercator.FromGeographic(new MapPoint(108.2, 37.1)),
Symbol = LayoutRoot.Resources["StrobeMarkerSymbol"] as Symbol
};
g.Attributes.Add("EFFECTIVE", "Helllo!");
g.Attributes.Add("HEADLINE", "fdafadfaf");
g.Attributes.Add("DESCRIPTION", "badfadfafaf");
glayer.Graphics.Add(g);
this.myMap.Layers.Add(glayer);
//鼠标悬停事件
glayer.MouseEnter += glayer_MouseEnter;
//鼠标移开事件
glayer.MouseLeave += glayer_MouseLeave;
}
4、处理鼠标事件,显示自定义窗体
void glayer_MouseLeave(object sender, ESRI.ArcGIS.Client.GraphicMouseEventArgs e)
{
elementLayer.Children.Clear();
//throw new System.NotImplementedException();
}
void glayer_MouseEnter(object sender, ESRI.ArcGIS.Client.GraphicMouseEventArgs e)
{
Graphic g = e.Graphic;
elementLayer.Children.Clear(); //清除上一次的展示界面
MyMapTip infoWindow = new MyMapTip(g.Attributes["EFFECTIVE"].ToString(), g.Attributes["HEADLINE"].ToString(), g.Attributes["DESCRIPTION"].ToString()); //实例化一个界面
//投影坐标系转换成地理坐标系(从WKID=102100到WKID=4326)
MapPoint mapPoint = mercator.ToGeographic(g.Geometry as ESRI.ArcGIS.Client.Geometry.MapPoint) as MapPoint;
//设置该参数来控制图显示的位置
ElementLayer.SetEnvelope(infoWindow, new Envelope(mapPoint.X, mapPoint.Y, mapPoint.X, mapPoint.Y));
elementLayer.Children.Add(infoWindow); //将界面添加到ElementLayer
//throw new System.NotImplementedException();
}