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">
<Grid Visibility="Collapsed" x:Name="gd1" Canvas.ZIndex="5" Height="30" Canvas.Left="1" Canvas.Top="1" Width="100" Background="#FFE9EC8F"/>
<ComboBox SelectionChanged="pick1" x:Name="cb1" Canvas.ZIndex="5" Canvas.Left="20" Canvas.Top="11" Width="120"/>
<ListBox x:Name="lb1" Height="158" Canvas.ZIndex="5" Canvas.Left="20" Canvas.Top="45" Width="120"/>
</Canvas>
</UserControl>
CS
using System.Linq;
using System.Net;
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;
using System.Collections.Generic;
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.Green);
LayoutRoot.Children.Add(m_pMapBase);
CachedLayer pLayer = new CachedLayer();
pLayer.ResourcePath = "http://sgs.supergeo.com.tw/svlcb/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);
lb1.Visibility = System.Windows.Visibility.Collapsed;
}
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(32.737241, 32.464221, 55.440381, 17.725127);
m_pMapBase.ZoomMapTo(pext1);
m_pMapBase.RefreshMap(true);
//fill up all the thing into the menu
List<ILayer> pLayers = m_pMapBase.Layers;
CachedLayer pLayer = pLayers[0] as CachedLayer;
List<innerLayer> pLayers2 = pLayer.Layers;
innerLayer pLayer2 = pLayers2[0];
string qryStr = "name != ''";
pLayer2.ExecuteQuery(qryStr, false, getCtyName, null);
m_pMapBase.OnRebuilt += UU;
}
private void getCtyName(XDocument pDoc)
{
this.Dispatcher.BeginInvoke(() =>
{
foreach (XElement pSubElem in pDoc.Descendants("Feature"))
{
XElement pValElem = pSubElem.Element("Values");
foreach (XElement pVal in pValElem.Elements())
{
}
XElement pGeomElem = pSubElem.Element("Geometry");
if (pGeomElem != null)
{
string sWKT = pGeomElem.Value;
SGWPoint po = new SGWPoint();
LineString ls = new LineString();
SGWPolygon pg = new SGWPolygon();
if (sWKT.StartsWith("POINT"))
{
po.ImportFromWKT(pGeomElem.Value);
XElement pValues = pSubElem.Element("Values");
XElement pValue1 = pValues.Element("NAME");
gd1.Children.Clear();
TextBlock tb1 = new TextBlock();
tb1.Text = pValue1.Value;
gd1.Children.Add(tb1);
Canvas.SetLeft(gd1, scrPt.X - 40);
Canvas.SetTop(gd1, scrPt.Y - 10);
gd1.Visibility = System.Windows.Visibility.Visible;
}
else if (sWKT.StartsWith("LINESTRING"))
{
ls.ImportFromWKT(pGeomElem.Value);
MessageBox.Show(ls.ExportToWKT());
}
else if (sWKT.StartsWith("POLYGON"))
{
pg.ImportFromWKT(pGeomElem.Value);
XElement pValues = pSubElem.Element("Values");
XElement pValue1 = pValues.Element("name");
cb1.Items.Add(pValue1.Value);
}
}
}
});
}
//ADD THIS FUNCTION
private void pick1(object sender, SelectionChangedEventArgs e)
{
//locate to the selected province
List<ILayer> pLayers = m_pMapBase.Layers;
CachedLayer pLayer = pLayers[0] as CachedLayer;
List<innerLayer> pLayers2 = pLayer.Layers;
innerLayer pLayer2 = pLayers2[0];
string qryStr = "name = '" + cb1.SelectedItem + "'";
pLayer2.ExecuteQuery(qryStr, false, QuerySucceed, null);
}
PointCollection myPtC = null;
private void QuerySucceed(XDocument pDoc)
{
lb1.Visibility = System.Windows.Visibility.Visible;
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");
lb1.Items.Clear();
lb1.Items.Add(pValue1.Value);
lb1.Items.Add(pValue2.Value);
lb1.Items.Add(pValue3.Value);
lb1.Items.Add(pValue4.Value);
pg.ImportFromWKT(pGeomElem.Value);
//draw the selected polygon
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;
}
}
}