XAML
<UserControl
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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="SilverlightApplication1.MainPage"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="LayoutRoot" Background="White" Margin="0,0,0,0">
</Canvas>
</UserControl>
CS
using System.Linq;
using System.Net;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;
using SGWSilverlightAPI.Interface;
using SGWSilverlightAPI.Map;
using SGWSilverlightAPI.Layer;
using SGWSilverlightAPI.Geometry;
using SGWPolygon = SGWSilverlightAPI.Geometry.Polygon;
using SGWPoint = SGWSilverlightAPI.Geometry.Point;
using System.IO;
using System.Windows.Media.Effects;
using SGWSilverlightAPI.Navigate;
using System;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
MapBase m_pMapBase = null;
System.Windows.Shapes.Path myPath = null;
public MainPage()
{
InitializeComponent();
myPath = new System.Windows.Shapes.Path();
m_pMapBase = new MapBase();
m_pMapBase.Width = 2000;
m_pMapBase.Height = 1000;
m_pMapBase.Background = new SolidColorBrush(Colors.White);
LayoutRoot.Children.Add(m_pMapBase);
MapLayer pLayer = new MapLayer();
pLayer.ResourcePath = "http://sgs.supergeo.com.tw/bufselsv/Agent.aspx";
pLayer.OnLayerLoaded += OnLayerLoaded;
m_pMapBase.AddLayer(pLayer);
PanTool pTool = new PanTool();
pTool.AllowDblClick = false;
pTool.AllowWheelZoom = true;
pTool.Initial(m_pMapBase);
m_pMapBase.SelectMapTool(pTool);
m_pMapBase.MouseLeftButtonUp += idfy;
}
System.Windows.Shapes.Polygon ep1 = null;
private void idfy(object sender, MouseButtonEventArgs e)
{
for (int i = m_pMapBase.Children.Count - 1; i > 0; i--)
{
m_pMapBase.Children.RemoveAt(i);
}
if (ep1 != null)
LayoutRoot.Children.Remove(ep1);
double R = 0.5; //search radius
List<ILayer> pLayers = m_pMapBase.Layers;
MapLayer pLayer = pLayers[0] as MapLayer;
List<innerLayer> pLayers2 = pLayer.Layers;
innerLayer pLayer2 = pLayers2[0];
SGWPoint pt1 = m_pMapBase.CursorPosition(e);
scrPt = pt1;
SGWPoint pt2 = m_pMapBase.ToMapPoint(pt1.X, pt1.Y);
curPt = pt2;
//create the "buffer"(circle) to query
ep1 = new System.Windows.Shapes.Polygon();
SolidColorBrush b1 = new SolidColorBrush();
b1.Color = Colors.Green;
ep1.Stroke = b1;
DropShadowEffect ef1 = new DropShadowEffect();
ef1.Color = Colors.Black;
ef1.Direction = 45;
ef1.ShadowDepth = 5;
ef1.BlurRadius = 6;
ef1.Opacity = 0.3;
SolidColorBrush b2 = new SolidColorBrush();
b2.Color = Colors.Red;
ep1.Opacity = 0.5;
ep1.Fill = b2;
ep1.Effect = ef1;
ep1.StrokeThickness = 2;
SGWPolygon pgn = new SGWPolygon();
for (int a = 0; a <= 360; a += 10)
{
double xp = pt2.X + Math.Sin(a * Math.PI / 180.0) * R;
double yp = pt2.Y + Math.Cos(a * Math.PI / 180.0) * R;
pgn.ExteriorRing.InsertPoint(-1, new SGWPoint(xp, yp));
}
for (int a = 0; a < pgn.ExteriorRing.PointCount - 1; a++)
{
SGWPoint tpt = new SGWPoint();
tpt = pgn.ExteriorRing.GetPoint(a);
System.Windows.Point p1 = new System.Windows.Point();
p1.X = m_pMapBase.FromMapPoint(tpt.X, tpt.Y).X;
p1.Y = m_pMapBase.FromMapPoint(tpt.X, tpt.Y).Y;
ep1.Points.Add(p1);
}
LayoutRoot.Children.Add(ep1);
pLayer2.ExecuteQuery(pgn.ExportToWKT(), true, sendLnQry, null);
}
List<string> pLst = null;
private void sendLnQry(XDocument pDoc)
{
pLst = new List<string>();
SGWPolygon pg = null;
int kk = 0;
foreach (XElement pSubElem in pDoc.Descendants("Feature"))
{
XElement pGeomElem = pSubElem.Element("Geometry");
if (pGeomElem != null)
{
kk = kk + 1;
string sWKT = pGeomElem.Value;
pg = new SGWPolygon();
pg.ImportFromWKT(sWKT);
myPtC = new PointCollection();
Path2 = new System.Windows.Shapes.Polygon();
XElement pValues = pSubElem.Element("Values");
XElement pValue1 = pValues.Element("name");
pLst.Add(pValue1.Value);
SolidColorBrush b1 = new SolidColorBrush();
b1.Color = Colors.Blue;
Path2.Stroke = b1;
DropShadowEffect ef1 = new DropShadowEffect();
ef1.Color = Colors.Black;
ef1.Direction = 45;
ef1.ShadowDepth = 5;
ef1.BlurRadius = 6;
ef1.Opacity = 0.3;
SolidColorBrush b2 = new SolidColorBrush();
b2.Color = Colors.Yellow;
b2.Opacity = 0.5;
Path2.Fill = b2;
Path2.Effect = ef1;
Path2.StrokeThickness = 2;
SGWPoint Spt = new SGWPoint();
SGWPoint Spt2 = new SGWPoint();
System.Windows.Point pt1 = new System.Windows.Point();
for (int i = 0; i < pg.ExteriorRing.PointCount - 1; i++)
{
Spt = pg.ExteriorRing.GetPoint(i);
Spt2 = m_pMapBase.FromMapPoint(Spt.X, Spt.Y);
pt1.X = Spt2.X;
pt1.Y = Spt2.Y;
Path2.Points.Add(pt1);
}
Path2.MouseEnter += Path2_MouseEnter;
Path2.DataContext = pLst;
m_pMapBase.Children.Add(Path2);
}
}
}
void Path2_MouseEnter(object sender, MouseEventArgs e)
{
//Do the spatial query again. to show the attribute data
List<ILayer> pLayers = m_pMapBase.Layers;
MapLayer pLayer = pLayers[0] as MapLayer;
List<innerLayer> pLayers2 = pLayer.Layers;
innerLayer pLayer2 = pLayers2[0];
SGWPoint pt1 = m_pMapBase.CursorPosition(e);
scrPt = pt1;
SGWPoint pt2 = m_pMapBase.ToMapPoint(pt1.X, pt1.Y);
pLayer2.ExecuteQuery(pt2.ExportToWKT(), true, shwMvr, null);
}
TextBlock tb1 = null;
private void shwMvr(XDocument pDoc)
{
//show tip on the map
if (tb1 != null)
m_pMapBase.Children.Remove(tb1);
foreach (XElement pSubElem in pDoc.Descendants("Feature"))
{
XElement pGeomElem = pSubElem.Element("Geometry");
if (pGeomElem != null)
{
XElement pValues = pSubElem.Element("Values");
XElement pValue1 = pValues.Element("name");
string sWKT = pGeomElem.Value;
SGWPolygon pg = new SGWPolygon();
pg.ImportFromWKT(sWKT);
tb1 = new TextBlock();
tb1.Text = pValue1.Value;
Canvas.SetZIndex(tb1, 5);
Canvas.SetLeft(tb1, (m_pMapBase.FromMapPoint(pg.Extent.Left, pg.Extent.Top)).X + 20);
Canvas.SetTop(tb1, (m_pMapBase.FromMapPoint(pg.Extent.Left, pg.Extent.Top)).Y + 20);
m_pMapBase.Children.Add(tb1);
}
}
}
SGWPoint curPt = null;
SGWPoint scrPt = null;
void m_pMapBase_MouseMove(object sender, MouseEventArgs e)
{
List<ILayer> pLayers = m_pMapBase.Layers;
MapLayer pLayer = pLayers[0] as MapLayer;
List<innerLayer> pLayers2 = pLayer.Layers;
innerLayer pLayer2 = pLayers2[0];
SGWPoint pt1 = m_pMapBase.CursorPosition(e);
scrPt = pt1;
SGWPoint pt2 = m_pMapBase.ToMapPoint(pt1.X, pt1.Y);
curPt = pt2;
SGWPolygon pgn = new SGWPolygon();
for (int a = 0; a <= 360; a += 30)
{
double xp = pt2.X + Math.Sin(a * Math.PI / 180.0) * 0.2;
double yp = pt2.Y + Math.Cos(a * Math.PI / 180.0) * 0.2;
pgn.ExteriorRing.InsertPoint(-1, new SGWPoint(xp, yp));
}
pLayer2.ExecuteQuery(pgn.ExportToWKT(), true, QuerySucceed, null);
m_pMapBase.MouseMove -= m_pMapBase_MouseMove;
}
private void OnLayerLoaded(object sender, EventArgs e)
{
Envelope pExt = m_pMapBase.Layers[0].Extent;
ITransformation pTrans = new ScaleTransformation(1);
m_pMapBase.Transformation = pTrans;
Envelope pext1 = new Envelope(5.327206, 55.738537, 15.995754, 46.996818);
m_pMapBase.ZoomMapTo(pext1);
m_pMapBase.RefreshMap(true);
}
PointCollection myPtC = null;
private void QuerySucceed(XDocument pDoc)
{
foreach (XElement pSubElem in pDoc.Descendants("Feature"))
{
XElement pGeomElem = pSubElem.Element("Geometry");
if (pGeomElem != null)
{
string sWKT = pGeomElem.Value;
SGWPolygon pg = new SGWPolygon();
pg.ImportFromWKT(pGeomElem.Value);
Spg = pg;
XElement pValues = pSubElem.Element("Values");
XElement pValue1 = pValues.Element("name");
XElement pValue2 = pValues.Element("name_alt");
XElement pValue3 = pValues.Element("type");
XElement pValue4 = pValues.Element("Shape_Area");
pg.ImportFromWKT(pGeomElem.Value);
SolidColorBrush b1 = new SolidColorBrush();
b1.Color = Colors.Blue;
myPath.Stroke = b1;
DropShadowEffect ef1 = new DropShadowEffect();
ef1.Color = Colors.Black;
ef1.Direction = 45;
ef1.ShadowDepth = 5;
ef1.BlurRadius = 6;
ef1.Opacity = 0.3;
SolidColorBrush b2 = new SolidColorBrush();
b2.Color = Colors.Yellow;
b2.Opacity = 0.5;
myPath.Fill = b2;
myPath.Effect = ef1;
myPath.StrokeThickness = 2;
myPtC = new PointCollection();
SGWPoint Spt = new SGWPoint();
SGWPoint Spt2 = new SGWPoint();
for (int i = 0; i < pg.ExteriorRing.PointCount - 1; i++)
{
Spt = pg.ExteriorRing.GetPoint(i);
Spt2 = m_pMapBase.FromMapPoint(Spt.X, Spt.Y);
myPtC.Add(new System.Windows.Point(Spt2.X, Spt2.Y));
}
PolyLineSegment myLineSegment = new PolyLineSegment();
myLineSegment.Points = myPtC;
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = myPtC[0];
myPathFigure.Segments = myPathSegmentCollection;
PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);
PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;
myPath.Data = myPathGeometry;
m_pMapBase.Children.Remove(myPath);
m_pMapBase.Children.Add(myPath);
m_pMapBase.RefreshMap(true);
}
}
}
SGWPolygon Spg = null;
private void UU()
{
if (Spg == null || myPath == null)
return;
PointCollection myPtC = new PointCollection();
SGWPoint Spt = new SGWPoint();
SGWPoint Spt2 = new SGWPoint();
for (int i = 0; i < Spg.ExteriorRing.PointCount - 1; i++)
{
Spt = Spg.ExteriorRing.GetPoint(i);
Spt2 = m_pMapBase.FromMapPoint(Spt.X, Spt.Y);
myPtC.Add(new System.Windows.Point(Spt2.X, Spt2.Y));
}
PolyLineSegment myLineSegment = new PolyLineSegment();
myLineSegment.Points = myPtC;
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = myPtC[0];
myPathFigure.Segments = myPathSegmentCollection;
PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);
PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;
myPath.Data = myPathGeometry;
}
System.Windows.Shapes.Polygon Path2 = null;
}
}