Skip to content

Commit cf4cda5

Browse files
committed
add support for Google Maps
1 parent 064eec1 commit cf4cda5

File tree

9 files changed

+197
-11
lines changed

9 files changed

+197
-11
lines changed

MainForm.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,18 @@ public MainForm()
106106
tracerouteGridView = new GridView { DataStore = tracerouteResultCollection };
107107
AddGridColumnsTraceroute();
108108

109-
mapWebView = new WebView
110-
{
111-
Url = new Uri("https://lbs.baidu.com/jsdemo/demo/webgl0_0.htm"),
112-
109+
mapWebView = new WebView();
110+
switch (UserSettings.mapProvider)
111+
{
112+
case "baidu":
113+
mapWebView.Url = new Uri("https://lbs.baidu.com/jsdemo/demo/webgl0_0.htm");
114+
break;
115+
case "google":
116+
mapWebView.Url = new Uri("https://geo-devrel-javascript-samples.web.app/samples/map-simple/app/dist/");
117+
break;
118+
}
119+
mapWebView.DocumentLoaded += (sender6, e6) => {
120+
ResetMap();
113121
};
114122

115123
// 绑定控件事件
@@ -454,7 +462,16 @@ private void FocusMapPoint(int hopNo)
454462
private void ResetMap()
455463
{
456464
// 重置或者初始化地图
457-
mapWebView.ExecuteScriptAsync(OpenTrace.Properties.Resources.baiduMap);
465+
switch (mapWebView.Url.Host)
466+
{
467+
case "geo-devrel-javascript-samples.web.app":
468+
mapWebView.ExecuteScriptAsync(OpenTrace.Properties.Resources.googleMap);
469+
break;
470+
case "lbs.baidu.com":
471+
mapWebView.ExecuteScriptAsync(OpenTrace.Properties.Resources.baiduMap);
472+
break;
473+
}
474+
mapWebView.ExecuteScriptAsync("window.opentrace.reset(" + UserSettings.hideMapPopup.ToString().ToLower() + ")");
458475
}
459476
private void AddGridColumnsTraceroute()
460477
{

OpenTrace.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<Title>OpenTrace</Title>
1515
<ApplicationIcon>icon.ico</ApplicationIcon>
1616
<PackageIcon>icon.png</PackageIcon>
17-
<Version>1.2.1.0</Version>
17+
<Version>1.2.2.0</Version>
1818

1919
</PropertyGroup>
2020

PreferencesDialog.xeto

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
<Label Text="{x:Static resource:Resources.MAP_PROVIDER}"></Label>
3838
<DropDown x:Name="mapProvider" SelectedIndex="0">
3939
<ListItem Text="{x:Static resource:Resources.MAP_PROVIDER_BAIDU}" Key="baidu"></ListItem>
40-
<!--
4140
<ListItem Text="{x:Static resource:Resources.MAP_PROVIDER_GOOGLE}" Key="google"></ListItem>
41+
<!--
4242
<ListItem Text="{x:Static resource:Resources.MAP_PROVIDER_OSM}" Key="openstreetmap"></ListItem>
4343
-->
4444
</DropDown>
@@ -51,6 +51,9 @@
5151
<TableRow>
5252
<CheckBox x:Name="timeRounding" Text="{x:Static resource:Resources.TIME_ROUNDING}"></CheckBox>
5353
</TableRow>
54+
<TableRow>
55+
<CheckBox x:Name="hideMapPopup" Text="{x:Static resource:Resources.HIDE_MAP_POPUP}"></CheckBox>
56+
</TableRow>
5457
<TableRow ScaleHeight="True"></TableRow>
5558
</TableLayout>
5659
</TabPage>

Properties/Resources.Designer.cs

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,15 @@
196196
<data name="GEOLOCATION" xml:space="preserve">
197197
<value>Geolocation</value>
198198
</data>
199+
<data name="googleMap" type="System.Resources.ResXFileRef, System.Windows.Forms">
200+
<value>..\javascript\googleMap.js;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
201+
</data>
199202
<data name="HELP" xml:space="preserve">
200203
<value>Help</value>
201204
</data>
205+
<data name="HIDE_MAP_POPUP" xml:space="preserve">
206+
<value>Hide the map's info windows</value>
207+
</data>
202208
<data name="HISTORY" xml:space="preserve">
203209
<value>History</value>
204210
</data>

Properties/Resources.zh-CN.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@
195195
<data name="HELP" xml:space="preserve">
196196
<value>帮助</value>
197197
</data>
198+
<data name="HIDE_MAP_POPUP" xml:space="preserve">
199+
<value>隐藏地图上的信息窗口</value>
200+
</data>
198201
<data name="HISTORY" xml:space="preserve">
199202
<value>历史记录</value>
200203
</data>

UserSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,8 @@ internal class UserSettings : Advexp.Settings<UserSettings>
6666

6767
[Setting(Name = "timeRounding", Default = false)]
6868
public static bool timeRounding { get; set; }
69+
70+
[Setting(Name = "hideMapPopup", Default = false)]
71+
public static bool hideMapPopup { get; set; }
6972
}
7073
}

javascript/baiduMap.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
window.opentrace = {
22
Hops: [],
3-
reset: function () {
3+
reset: function (hideMapPopup = false) {
44
map.enableScrollWheelZoom(true); //滚轮
55
map.clearOverlays(); //清除覆盖物
66
this.Hops = [];
7+
this.hideMapPopup = hideMapPopup;
78
if (document.getElementById("opentracePopup")) document.getElementById("opentracePopup").remove();
89
},
910

@@ -39,14 +40,17 @@ window.opentrace = {
3940
var hop = this.Hops[hopNo];
4041
if (hop == null) return;
4142
this.showPopup(hopNo);
43+
if (hop.Longitude == 0 || hop.Latitude == 0 || hop.Longitude == "" || hop.Latitude == "") return;
4244
var point = new BMapGL.Point(hop.Longitude, hop.Latitude);
4345
map.centerAndZoom(point, 8);
46+
4447
},
4548

4649
showPopup: function (hopNo) {
4750
var hop = this.Hops[hopNo];
4851
if (hop == null) return;
4952
if (document.getElementById("opentracePopup")) document.getElementById("opentracePopup").remove();
53+
if (this.hideMapPopup == true) return;
5054
// 创建左上角的悬浮提示
5155
var popupElement = document.createElement("div");
5256
popupElement.id = "opentracePopup";
@@ -83,6 +87,4 @@ window.opentrace = {
8387
</table>`;
8488
document.body.appendChild(popupElement);
8589
},
86-
};
87-
88-
window.opentrace.reset();
90+
};

javascript/googleMap.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
window.opentrace = {
2+
Hops: [],
3+
reset: function (hideMapPopup = false) {
4+
if (typeof gmap == "undefined") window.gmap = new google.maps.Map(document.getElementById("map"), { center: { lat: 0, lng: 0 }, zoom: 2 });
5+
gmap.overlayMapTypes.clear();
6+
this.Hops = [];
7+
this.hideMapPopup = hideMapPopup;
8+
if (document.getElementById("opentracePopup")) document.getElementById("opentracePopup").remove();
9+
},
10+
11+
addHop: function (hop) {
12+
13+
// Parse the JSON string into an object
14+
const hopData = JSON.parse(hop);
15+
16+
console.log(hopData);
17+
18+
// Clear any existing overlays
19+
gmap.overlayMapTypes.clear();
20+
21+
// Add the new hop to the list of hops
22+
this.Hops.push(hopData);
23+
24+
// Loop through all hops and add markers
25+
for (let i = 0; i < this.Hops.length; i++) {
26+
if (this.Hops[i].Longitude == 0 || this.Hops[i].Latitude == 0 || this.Hops[i].Longitude == "" || this.Hops[i].Latitude == "") continue;
27+
let h = this.Hops[i];
28+
let latLng = new google.maps.LatLng(h.Latitude, h.Longitude);
29+
let marker = new google.maps.Marker({
30+
position: latLng,
31+
map: gmap,
32+
title: `#${h.No}: ${h.Geolocation}`
33+
});
34+
let markerIndex = i;
35+
google.maps.event.addListener(marker, 'mouseover', () => {
36+
this.showPopup(markerIndex);
37+
});
38+
}
39+
40+
// Connect the markers with a polyline
41+
let path = [];
42+
for (let i = 0; i < this.Hops.length; i++) {
43+
if (this.Hops[i].Longitude == 0 || this.Hops[i].Latitude == 0 || this.Hops[i].Longitude == "" || this.Hops[i].Latitude == "") continue;
44+
let h = this.Hops[i];
45+
let latLng = new google.maps.LatLng(h.Latitude, h.Longitude);
46+
path.push(latLng);
47+
}
48+
let polyline = new google.maps.Polyline({
49+
path: path,
50+
strokeColor: '#FF0000',
51+
strokeOpacity: 1.0,
52+
strokeWeight: 2
53+
});
54+
polyline.setMap(gmap);
55+
56+
if (path.length > 1) {
57+
// Get the bounds of the path
58+
let bounds = new google.maps.LatLngBounds();
59+
for (let i = 0; i < path.length; i++) {
60+
bounds.extend(path[i]);
61+
}
62+
// Fit the map to those bounds
63+
gmap.fitBounds(bounds);
64+
}
65+
},
66+
67+
focusHop: function (hopNo) {
68+
var hop = this.Hops[hopNo];
69+
if (hop == null) return;
70+
this.showPopup(hopNo);
71+
if (hop.Longitude == 0 || hop.Latitude == 0 || hop.Longitude == "" || hop.Latitude == "") return;
72+
let hopPos = new google.maps.LatLng(hop.Latitude, hop.Longitude);
73+
gmap.setCenter(hopPos);
74+
},
75+
76+
showPopup: function (hopNo) {
77+
var hop = this.Hops[hopNo];
78+
if (hop == null) return;
79+
if (document.getElementById("opentracePopup")) document.getElementById("opentracePopup").remove();
80+
if (this.hideMapPopup == true) return;
81+
// 创建左上角的悬浮提示
82+
var popupElement = document.createElement("div");
83+
popupElement.id = "opentracePopup";
84+
popupElement.style =
85+
"position: absolute; bottom: 10px; max-width: 33%; left: 10px; padding: 5px; background-color: rgba(255,255,255,0.85); border: 1px solid #ccc; border-radius: 5px; z-index: 9999;";
86+
popupElement.innerHTML = `
87+
<style>
88+
td:first-child {
89+
font-weight:bold;
90+
padding-right:5px;
91+
}
92+
</style>
93+
<table>
94+
<tr>
95+
<td style="white-space: nowrap;">IP #${hop.No}</td>
96+
<td>${hop.IP}</td>
97+
</tr>
98+
<tr>
99+
<td>Time</td>
100+
<td>${hop.Time}</td>
101+
</tr>
102+
<tr>
103+
<td>Geo</td>
104+
<td>${hop.Geolocation}</td>
105+
</tr>
106+
<tr>
107+
<td>Org</td>
108+
<td>${hop.Organization}</td>
109+
</tr>
110+
<tr>
111+
<td>AS</td>
112+
<td>${hop.AS}</td>
113+
</tr>
114+
</table>`;
115+
document.body.appendChild(popupElement);
116+
},
117+
};
118+

0 commit comments

Comments
 (0)