<UserControl x:Class="infowin1.MainPage"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="LayoutRoot" Background="White" Margin="10, 10,10,-100">
<Grid Visibility="Collapsed" x:Name="gd1" Canvas.ZIndex="5" Height="60" Canvas.Left="1" Canvas.Top="1" Width="100" Background="#FFE9EC8F"/>
</Canvas>
</UserControl>
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 infowin1
{
public partial class MainPage : UserControl
{
MapBase m_pMapBase = null;
public MainPage()
{
InitializeComponent();
m_pMapBase = new MapBase();
m_pMapBase.Width = 600;
m_pMapBase.Height = 400;
m_pMapBase.Background = new SolidColorBrush(Colors.Green);
LayoutRoot.Children.Add(m_pMapBase);
CachedLayer pLayer = new CachedLayer();
pLayer.ResourcePath = "http://sgs.supergeo.com.tw/slsagtinfowin/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);
//ADD MOUSE CLICK EVENT
m_pMapBase.MouseLeftButtonUp += m_pMapBase_MouseLeftButtonUp;
}
SGWPoint curPt = null;
void m_pMapBase_MouseLeftButtonUp(object sender, MouseButtonEventArgs 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);
SGWPoint pt2 = m_pMapBase.ToMapPoint(pt1.X, pt1.Y);
curPt = pt2;
//PERFORM WKT QUERY AFTER MOUSE CLICKED
pLayer2.ExecuteQuery(pt2.ExportToWKT(), true, QuerySucceed,null);
Canvas.SetLeft(gd1, m_pMapBase.CursorPosition(e).X-40);
Canvas.SetTop(gd1, m_pMapBase.CursorPosition(e).Y-60);
}
private void QuerySucceed(XDocument pDoc)
{
int kk = pDoc.ToString().Length;
if (kk == 10) //that means you click on the empty area.
{
gd1.Children.Clear();
TextBlock tb1 = new TextBlock();
tb1.Text = curPt.X.ToString() + "\n" + curPt.Y.ToString();
gd1.Children.Add(tb1);
gd1.Visibility = System.Windows.Visibility.Visible;
}
this.Dispatcher.BeginInvoke(() =>
{
foreach (XElement pSubElem in pDoc.Descendants("Feature"))
{
// string strName = "";
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);
MessageBox.Show(po.ExportToWKT());
}
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("SOVEREIGNT");
XElement pValue2 = pValues.Element("POP_EST");
XElement pValue3 = pValues.Element("GDP_MD_EST");
gd1.Children.Clear();
TextBlock tb1 = new TextBlock();
tb1.Text = pValue1.Value + "\nPop: " + pValue2.Value + "\nGDP: " + pValue3.Value;
gd1.Children.Add(tb1);
gd1.Visibility = System.Windows.Visibility.Visible;
}
}
}
});
}
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(64.055019, 32.082678, 90.826878, 8.949713);
m_pMapBase.ZoomMapTo(pExt);
m_pMapBase.Scale = m_pMapBase.Scale * 2;
m_pMapBase.RefreshMap(true);
}
}
public class MeasureLengthTool : Button, ITool
{
private MapBase m_pMapBase = null;
private PointTracker ptTk = null;
public MeasureLengthTool()
{
}
public void Initial(MapBase MapBase)
{
m_pMapBase = MapBase;
}
public bool InitMapEvent()
{
m_pMapBase.MouseLeftButtonDown += OnMouseDown;
return true;
}
public bool ExitMapEvent()
{
if (ptTk != null)
ptTk.Terminate();
ptTk = null;
m_pMapBase.MouseLeftButtonDown -= OnMouseDown;
return true;
}
public void MapCommand(MapBase MapBase)
{
}
public void Launch()
{
OnClick();
}
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (ptTk == null)
ptTk = new PointTracker(m_pMapBase, e, End);
}
private void End()
{
IGeometry Geom = ptTk.Geometry;
MessageBox.Show(Geom.ExportToWKT());
ptTk.Terminate();
ptTk = null;
List<ILayer> pLayers = m_pMapBase.Layers;
MapLayer pLayer = pLayers[0] as MapLayer;
List<innerLayer> pLayers2 = pLayer.Layers;
innerLayer pLayer2 = pLayers2[0];
pLayer2.ExecuteQuery(Geom.ExportToWKT(), true, QuerySucceed, null);
}
private void QuerySucceed(XDocument pDoc)
{
MessageBox.Show(pDoc.ToString());
this.Dispatcher.BeginInvoke(() =>
{
foreach (XElement pSubElem in pDoc.Descendants("Feature"))
{
// string strName = "";
XElement pValElem = pSubElem.Element("Values");
foreach (XElement pVal in pValElem.Elements())
{
MessageBox.Show(pVal.Name + "," + pVal.Value);
}
XElement pGeomElem = pSubElem.Element("Geometry");
if (pGeomElem != null)
{
string sWKT = pGeomElem.Value;
MessageBox.Show(sWKT);
SGWPoint po = new SGWPoint();
LineString ls = new LineString();
SGWPolygon pg = new SGWPolygon();
if (sWKT.StartsWith("POINT"))
{
po.ImportFromWKT(pGeomElem.Value);
MessageBox.Show(po.ExportToWKT());
}
else if (sWKT.StartsWith("LINESTRING"))
{
ls.ImportFromWKT(pGeomElem.Value);
MessageBox.Show(ls.ExportToWKT());
}
else if (sWKT.StartsWith("POLYGON"))
{
MessageBox.Show("aa");
pg.ImportFromWKT(pGeomElem.Value);
XElement pValues = pSubElem.Element("Values");
XElement pValue = pValues.Element("COUNTYNAME");
MessageBox.Show(pValue.Value);
m_pMapBase.ZoomMapTo(pg.Extent);
m_pMapBase.RefreshMap(true);
}
}
}
});
}
}
public class PointTracker : ITracker
{
#region Declare
private MapBase m_MapBase = null;
private SGWPoint m_MouseDownPoint = new SGWPoint();
private TrackCallBack m_CallBackFunc = null;
private SGWPolygon m_Geom = null;
private Ellipse m_Ellipse = null;
private double m_Size = 6.0;
#endregion
#region Constructor
public PointTracker(MapBase MapBase, MouseEventArgs e, TrackCallBack CallBackFunc)
{
m_MapBase = MapBase;
m_CallBackFunc = CallBackFunc;
m_MouseDownPoint = m_MapBase.CursorPosition(e);
//Mouse event
m_MapBase.MouseLeftButtonUp += OnMouseUp;
m_MapBase.CaptureMouse();
//Using Ellipse as a point
m_Ellipse = new Ellipse();
SolidColorBrush FillColor = new SolidColorBrush(Colors.Blue);
FillColor.Opacity = 1;
m_Ellipse.Fill = FillColor;
m_Ellipse.StrokeThickness = 0;
m_Ellipse.Height = m_Ellipse.Width = m_Size;
m_MapBase.Object.Children.Add(m_Ellipse); //Add to Mapbase canvas
Canvas.SetLeft(m_Ellipse, m_MouseDownPoint.X - (m_Ellipse.Width / 2));
Canvas.SetTop(m_Ellipse, m_MouseDownPoint.Y - (m_Ellipse.Height / 2));
}
#endregion
#region Attribute and Method
//Attribute
public IGeometry Geometry
{
get { return m_Geom; }
}
//Method
protected virtual void OnMouseUp(object sender, MouseEventArgs e)
{
m_MapBase.ReleaseMouseCapture();
//Left Top Point and Right Bottom Point
SGWPoint cpt1 = m_MapBase.ToMapPoint(m_MouseDownPoint.X - (m_Size / 2), m_MouseDownPoint.Y - (m_Size / 2));
SGWPoint cpt2 = m_MapBase.ToMapPoint(m_MouseDownPoint.X + (m_Size / 2), m_MouseDownPoint.Y + (m_Size / 2));
//Create Geometry
m_Geom = new SGWPolygon();
m_Geom.ExteriorRing.InsertPoint(-1, cpt1);
m_Geom.ExteriorRing.InsertPoint(-1, new SGWPoint(cpt2.X, cpt1.Y));
m_Geom.ExteriorRing.InsertPoint(-1, cpt2);
m_Geom.ExteriorRing.InsertPoint(-1, new SGWPoint(cpt1.X, cpt2.Y));
if (m_CallBackFunc != null)
m_CallBackFunc();
Terminate();
}
public void Terminate()
{
m_MapBase.Object.Children.Remove(m_Ellipse);
m_MapBase.MouseLeftButtonUp -= OnMouseUp;
}
#endregion
}
}