-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathForm1.cs
178 lines (147 loc) · 6.35 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Shapes;
using Ptv.XServer.Controls.Map.Layers;
using Ptv.XServer.Controls.Map.Layers.Shapes;
using Ptv.XServer.Controls.Map.Layers.Xmap2;
using Ptv.XServer.Controls.Map.TileProviders;
namespace XMap2FactoryTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var center = new Point(8.4044, 49.01405);
formsMap.SetMapLocation(center, 15);
formsMap.MaxZoom = 22;
formsMap.XMapUrl = "xserver2-europe-eu-test;version=2.8";
formsMap.XMapCredentials = "Insert your xToken here";
InitializeXMap2();
formsMap.Layers.InsertBefore(CreateShapeLayer(center), "Labels"); // Custom shape layer in-between background and labels
formsMap.ToolTipManagement.CreateCustomizedToolTipsFunc = CreateOwnToolTip;
formsMap.ToolTipManagement.DestroyCustomizedToolTipsFunc = DestroyOwnToolTip;
}
private void InitializeXMap2()
{
DemonstrationHowToModifyXServerRequestBeforeItIsSent();
DemonstrationHowToGetDetailedInformationOfXServerException();
var layerFactory = formsMap.Xmap2LayerFactory;
if (layerFactory == null)
return; // An invalid xServer2-URL was specified, the following XServer2 preparations cannot be executed.
PrepareMapStyles(layerFactory);
PrepareLanguages(layerFactory);
new FeatureLayerSetter(this);
}
private void DemonstrationHowToModifyXServerRequestBeforeItIsSent()
{
LayerFactory.ModifyRequest = request =>
{
request.Headers["Dummy"] = "Test";
AppendTextBox(request.RequestUri.AbsoluteUri);
AppendTextBox(request.Headers.ToString());
return request;
};
}
private void DemonstrationHowToGetDetailedInformationOfXServerException()
{
LayerFactory.ReportXServerError = exception =>
{
using (var stream = exception.Response.GetResponseStream())
if (stream != null)
using (var reader = new StreamReader(stream, Encoding.UTF8))
AppendTextBox(reader.ReadToEnd());
};
}
private void PrepareMapStyles(LayerFactory layerFactory)
{
foreach (var mapStyle in layerFactory.AvailableMapStyles)
if (mapStyle.Equals("default")) // if "default" is available then insert it at first position.
mapStylesComboBox.Items.Insert(0, mapStyle);
else
mapStylesComboBox.Items.Add(mapStyle);
mapStylesComboBox.SelectedIndex = Math.Min(0, mapStylesComboBox.Items.Count - 1);
mapStylesComboBox.SelectedIndexChanged += (_, __) => layerFactory.MapStyle = mapStylesComboBox.Text;
layerFactory.MapStyle = mapStylesComboBox.Text;
}
private void PrepareLanguages(LayerFactory layerFactory)
{
mapLanguageTextBox.Leave += (_, __) => layerFactory.MapLanguage = mapLanguageTextBox.Text;
layerFactory.MapLanguage = mapLanguageTextBox.Text;
trafficIncidentsLanguageTextBox.Leave += (_, __) => layerFactory.UserLanguage = trafficIncidentsLanguageTextBox.Text;
layerFactory.UserLanguage = trafficIncidentsLanguageTextBox.Text;
}
public void AppendTextBox(string value)
{
if (InvokeRequired)
BeginInvoke(new Action<string>(AppendTextBox), value);
else
requestLoggingTextBox.AppendText(value);
}
private void CreateOwnToolTip(List<IMapObject> toolTipMapObjects)
{
var stackPanel = new StackPanel();
foreach (var item in toolTipMapObjects.Select((toolTipMapObject, index) => new { index, toolTipMapObject }))
{
var label = new System.Windows.Controls.Label
{
Margin = new Thickness(1),
Foreground = new SolidColorBrush(Colors.Blue),
Content = item.toolTipMapObject.ToString()
};
stackPanel.Children.Add(item.index == 0
? (UIElement) label
: new Border
{
BorderThickness = new Thickness(0, 1, 0, 0),
BorderBrush = new SolidColorBrush(Colors.White),
Child = label
});
}
// create and show tool tip
customizedToolTip = new System.Windows.Controls.ToolTip
{
Content = stackPanel,
IsOpen = true
};
}
private void DestroyOwnToolTip(List<IMapObject> toolTipMapObjects)
{
if (customizedToolTip == null) return;
// close tool tip
customizedToolTip.IsOpen = false;
customizedToolTip = null;
}
private System.Windows.Controls.ToolTip customizedToolTip;
private static ILayer CreateShapeLayer(Point center)
{
var shapeLayer = new ShapeLayer("Shape layer") {Copyright = "DDS"};
AddCircle(shapeLayer, center, 500);
return shapeLayer;
}
private static void AddCircle(ShapeLayer layer, Point center, double radius)
{
// calculate the size in mercator units
var cosB = Math.Cos(center.Y * Math.PI / 180.0); // factor depends on latitude
var ellipseSize = Math.Abs(1.0 / cosB * radius) * 2; // size mercator units
var ellipse = new Ellipse
{
Width = ellipseSize,
Height = ellipseSize,
Fill = new SolidColorBrush(Color.FromArgb(128, 0, 0, 255)),
Stroke = new SolidColorBrush(Colors.LightBlue),
StrokeThickness = 25
};
ShapeCanvas.SetScaleFactor(ellipse, 1); // scale linear
ShapeCanvas.SetLocation(ellipse, center);
layer.Shapes.Add(ellipse);
}
}
}