Skip to content

Commit 490a64e

Browse files
Merge branch 'map_in_browser'
2 parents 4f0e878 + 0858c49 commit 490a64e

File tree

5 files changed

+161
-1
lines changed

5 files changed

+161
-1
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!DOCTYPE HTML>
2+
<!--
3+
COPYRIGHT 2009, 2010, 2011, 2012, 2013, 2014 by the Open Rails project.
4+
5+
This file is part of Open Rails.
6+
7+
Open Rails is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License as published by
9+
the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
Open Rails is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
19+
-->
20+
<html lang="en">
21+
<head>
22+
<title>OR: Map</title>
23+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
24+
25+
<link rel="stylesheet"
26+
href="https://unpkg.com/[email protected]/dist/leaflet.css"
27+
integrity="sha256-sA+zWATbFveLLNqWO2gtiw3HL/lh1giY/Inf1BJ0z14="
28+
crossorigin="">
29+
30+
<script src="https://unpkg.com/[email protected]/dist/jquery.min.js"
31+
integrity="sha384-vtXRMe3mGCbOeY7l30aIg8H9p3GdeSe4IFlP6G8JMa7o7lXvnz3GFKzPxzJdPfGK"
32+
crossorigin="anonymous"></script>
33+
34+
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
35+
integrity="sha256-o9N1jGDZrf5tS+Ft4gbIK7mYMipq9lqpVJ91xHSyKhg="
36+
crossorigin=""></script>
37+
38+
<style type="text/css">
39+
html, body {
40+
height: 99%;
41+
width: 99%;
42+
margin: 10px;
43+
padding: 0;
44+
}
45+
46+
#map {
47+
height: 99%;
48+
min-height: 100%;
49+
}
50+
</style>
51+
</head>
52+
53+
<body onload="setInterval (ApiMap, 500)">
54+
<div id="map"></div>
55+
<script src="index.js"></script>
56+
</body>
57+
</html>
58+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// COPYRIGHT 2009, 2010, 2011, 2012, 2013, 2014 by the Open Rails project.
2+
//
3+
// This file is part of Open Rails.
4+
//
5+
// Open Rails is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Open Rails is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
17+
//
18+
19+
var hr = new XMLHttpRequest;
20+
var httpCodeSuccess = 200;
21+
var xmlHttpRequestCodeDone = 4;
22+
23+
var locomotivMarker;
24+
var map;
25+
var latLongPrev = [0, 0];
26+
27+
function MapInit(latLong) {
28+
29+
map = L.map('map').setView(latLong, 13);
30+
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
31+
maxZoom: 19,
32+
attribution: 'Map data: &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
33+
}).addTo(map);
34+
35+
L.tileLayer('https://{s}.tiles.openrailwaymap.org/standard/{z}/{x}/{y}.png', {
36+
maxZoom: 19,
37+
attribution: ' | Map style: &copy; <a href="https://www.OpenRailwayMap.org">OpenRailwayMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)'
38+
}).addTo(map);
39+
}
40+
41+
function ApiMap() {
42+
43+
hr.open("GET", "/API/MAP/", true);
44+
hr.send();
45+
hr.onreadystatechange = function () {
46+
if (this.readyState == xmlHttpRequestCodeDone && this.status == httpCodeSuccess) {
47+
var responseText = JSON.parse(hr.responseText);
48+
if (responseText.length > 0) {
49+
latLong = responseText.split(" ");
50+
if (typeof locomotivMarker !== 'undefined') {
51+
if ((latLong[0] != latLongPrev[0]) && (latLong[1] != latLongPrev[1])) {
52+
map.panTo(latLong);
53+
}
54+
} else {
55+
MapInit(latLong);
56+
}
57+
58+
if ((latLong[0] != latLongPrev[0]) && (latLong[1] != latLongPrev[1])) {
59+
if (typeof locomotivMarker !== 'undefined') {
60+
locomotivMarker.removeFrom(map);
61+
}
62+
locomotivMarker = L.marker(
63+
latLong,
64+
{ icon: myIcon }
65+
).addTo(map);
66+
latLongPrev[0] = latLong[0];
67+
latLongPrev[1] = latLong[1];
68+
}
69+
}
70+
}
71+
}
72+
}
73+
74+
var myIcon = L.icon({
75+
iconUrl: 'locomotiv.png',
76+
iconSize: [29, 24],
77+
iconAnchor: [9, 21],
78+
})
Loading

Source/RunActivity/Viewer3D/WebServices/Web/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ <h1>Open Rails - Web Interface</h1>
3737
<li><a href="/HUD/index.html">Head Up Display (Alt+F5)</a></li>
3838
<li><a href="/CabControls/index.html">Cab Controls</a></li>
3939
<li><a href="/Time/index.html">Time</a></li>
40+
<li><a href="/Map/index.html">Map</a></li>
4041
<li><a href="/APISample/index.html">API Sample</a></li>
4142
</ul>
4243
</body>

Source/RunActivity/Viewer3D/WebServices/WebServer.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
using Microsoft.Xna.Framework;
2727
using Newtonsoft.Json;
2828
using Newtonsoft.Json.Serialization;
29+
using Orts.Common;
2930
using Orts.Simulation.Physics;
3031
using Orts.Viewer3D.RollingStock;
32+
using ORTS.Common;
3133
using System;
3234
using System.Collections.Generic;
3335
using System.Linq;
@@ -112,12 +114,28 @@ internal class ORTSApiController : WebApiController
112114
/// The Viewer to serve train data from.
113115
/// </summary>
114116
private readonly Viewer Viewer;
117+
private bool latLongCorrToBeRead = true;
118+
protected WorldLocation cameraLocation = new WorldLocation();
115119

116120
public ORTSApiController(Viewer viewer)
117121
{
118122
Viewer = viewer;
119123
}
120124

125+
public string getLatLong()
126+
{
127+
double latitude = 0;
128+
double longitude = 0;
129+
130+
var playerLocation = Viewer.Simulator.PlayerLocomotive.WorldPosition.WorldLocation;
131+
132+
new WorldLatLon().ConvertWTC(playerLocation.TileX, playerLocation.TileZ, playerLocation.Location, ref latitude, ref longitude);;
133+
134+
string latitudeStr = (MathHelper.ToDegrees((float)latitude)).ToString("F6").Replace(',', '.');
135+
string longitudeStr = (MathHelper.ToDegrees((float)longitude)).ToString("F6").Replace(',', '.');
136+
137+
return (latitudeStr + " " + longitudeStr);
138+
}
121139

122140
#region /API/APISAMPLE
123141
public struct Embedded
@@ -254,5 +272,10 @@ IEnumerable<string> GetValues()
254272
[Route(HttpVerbs.Get, "/TIME")]
255273
public double Time() => Viewer.Simulator.ClockTime;
256274
#endregion
275+
276+
#region /API/MAP
277+
[Route(HttpVerbs.Get, "/MAP")]
278+
public string LatLong() => getLatLong();
279+
#endregion
257280
}
258-
}
281+
}

0 commit comments

Comments
 (0)