Skip to content

Commit d2e6821

Browse files
authored
Merge pull request #749 from sweiland-openrails/map_in_browser
OpenRailway Map
2 parents 084d232 + db5764c commit d2e6821

File tree

7 files changed

+187
-4
lines changed

7 files changed

+187
-4
lines changed

Source/Orts.Simulation/Common/WorldLatLon.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public class WorldLatLon
4545

4646
// The upper left corner of the Goode projection is ul_x,ul_y
4747
// The bottom right corner of the Goode projection is -ul_x,-ul_y
48-
int ul_x = -20015000; // -180 deg in Goode projection
49-
int ul_y = 8673000; // +90 deg lat in Goode projection
48+
int ul_x = -20013965; // -180 deg in Goode projection
49+
int ul_y = 8674008; // +90 deg lat in Goode projection
5050

5151
// Offsets to convert Goode raster coordinates to MSTS world tile coordinates
5252
int wt_ew_offset = -16385;
@@ -287,4 +287,22 @@ static double Adjust_Lon(double value)
287287
}
288288

289289
}
290+
291+
/// <summary>
292+
/// Class to store the latitude and longitude values of a point on the map
293+
/// </summary>
294+
public class LatLon
295+
{
296+
private readonly float _lat;
297+
private readonly float _lon;
298+
299+
public LatLon(float lat, float lon)
300+
{
301+
this._lat = lat;
302+
this._lon = lon;
303+
}
304+
305+
public float Lat => _lat;
306+
public float Lon => _lon;
307+
}
290308
}

Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
//#define DEBUG_BRAKE_SLIDE
3535

3636
using Microsoft.Xna.Framework;
37+
using Orts.Common;
3738
using Orts.Formats.Msts;
3839
using Orts.Parsers.Msts;
3940
using Orts.Simulation.AIs;
@@ -3348,6 +3349,25 @@ public virtual void UpdateHeatLoss()
33483349

33493350
TotalCarCompartmentHeatLossW = HeatLossTransmissionW + HeatLossInfiltrationW + HeatLossVentilationW;
33503351
}
3352+
3353+
/// <summary>
3354+
/// Determine latitude/longitude position of the current TrainCar
3355+
/// </summary>
3356+
public LatLon GetLatLon()
3357+
{
3358+
double lat = 0;
3359+
double lon = 0;
3360+
3361+
var playerLocation = WorldPosition.WorldLocation;
3362+
3363+
new WorldLatLon().ConvertWTC(playerLocation.TileX, playerLocation.TileZ, playerLocation.Location, ref lat, ref lon);
3364+
3365+
LatLon latLon = new LatLon(
3366+
MathHelper.ToDegrees((float)lat),
3367+
MathHelper.ToDegrees((float)lon));
3368+
3369+
return (latLon);
3370+
}
33513371
}
33523372

33533373
public class WheelAxle : IComparer<WheelAxle>
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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
const hr = new XMLHttpRequest;
20+
const httpCodeSuccess = 200;
21+
const xmlHttpRequestCodeDone = 4;
22+
23+
var locomotiveMarker;
24+
var map;
25+
var latLonPrev = [0, 0];
26+
27+
function MapInit(latLon) {
28+
29+
map = L.map('map').setView(latLon, 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+
48+
let latLonObj = JSON.parse(hr.responseText);
49+
50+
if (latLonObj != null) {
51+
52+
let latLon = [latLonObj.Lat, latLonObj.Lon];
53+
54+
if (typeof locomotiveMarker == 'undefined') {
55+
// init
56+
MapInit(latLon);
57+
locomotiveMarker = L.marker(
58+
latLon,
59+
{ icon: myIcon }
60+
).addTo(map);
61+
} else {
62+
if ((latLon[0] != latLonPrev[0]) || (latLon[1] != latLonPrev[1])) {
63+
// changed
64+
map.panTo(latLon);
65+
locomotiveMarker.setLatLng(latLon).update();
66+
}
67+
}
68+
latLonPrev[0] = latLon[0];
69+
latLonPrev[1] = latLon[1];
70+
}
71+
}
72+
}
73+
}
74+
75+
var myIcon = L.icon({
76+
iconUrl: 'locomotive.png',
77+
iconSize: [29, 24],
78+
iconAnchor: [9, 21],
79+
})
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: 9 additions & 2 deletions
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,13 +114,13 @@ internal class ORTSApiController : WebApiController
112114
/// The Viewer to serve train data from.
113115
/// </summary>
114116
private readonly Viewer Viewer;
117+
protected WorldLocation cameraLocation = new WorldLocation();
115118

116119
public ORTSApiController(Viewer viewer)
117120
{
118121
Viewer = viewer;
119122
}
120123

121-
122124
#region /API/APISAMPLE
123125
public struct Embedded
124126
{
@@ -254,5 +256,10 @@ IEnumerable<string> GetValues()
254256
[Route(HttpVerbs.Get, "/TIME")]
255257
public double Time() => Viewer.Simulator.ClockTime;
256258
#endregion
259+
260+
#region /API/MAP
261+
[Route(HttpVerbs.Get, "/MAP")]
262+
public LatLon LatLon() => Viewer.Simulator.PlayerLocomotive.GetLatLon();
263+
#endregion
257264
}
258-
}
265+
}

0 commit comments

Comments
 (0)