Skip to content

Commit db5764c

Browse files
Created class LatLon. Created function GetLatLon in TrainCar. Reworked index.js. Returned LatLon object for /API/MAP.
1 parent 7dab270 commit db5764c

File tree

4 files changed

+61
-37
lines changed

4 files changed

+61
-37
lines changed

Source/Orts.Simulation/Common/WorldLatLon.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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>

Source/RunActivity/Viewer3D/WebServices/Web/Map/index.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717
//
1818

19-
var hr = new XMLHttpRequest;
20-
var httpCodeSuccess = 200;
21-
var xmlHttpRequestCodeDone = 4;
19+
const hr = new XMLHttpRequest;
20+
const httpCodeSuccess = 200;
21+
const xmlHttpRequestCodeDone = 4;
2222

23-
var locomotivMarker;
23+
var locomotiveMarker;
2424
var map;
2525
var latLonPrev = [0, 0];
2626

@@ -44,28 +44,29 @@ function ApiMap() {
4444
hr.send();
4545
hr.onreadystatechange = function () {
4646
if (this.readyState == xmlHttpRequestCodeDone && this.status == httpCodeSuccess) {
47-
var responseText = JSON.parse(hr.responseText);
48-
if (responseText.length > 0) {
49-
latLon = responseText.split(" ");
50-
if (typeof locomotivMarker !== 'undefined') {
51-
if ((latLon[0] != latLonPrev[0]) || (latLon[1] != latLonPrev[1])) {
52-
map.panTo(latLon);
53-
}
54-
} else {
55-
MapInit(latLon);
56-
}
5747

58-
if ((latLon[0] != latLonPrev[0]) || (latLon[1] != latLonPrev[1])) {
59-
if (typeof locomotivMarker !== 'undefined') {
60-
locomotivMarker.removeFrom(map);
61-
}
62-
locomotivMarker = L.marker(
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(
6358
latLon,
6459
{ icon: myIcon }
6560
).addTo(map);
66-
latLonPrev[0] = latLon[0];
67-
latLonPrev[1] = latLon[1];
61+
} else {
62+
if ((latLon[0] != latLonPrev[0]) || (latLon[1] != latLonPrev[1])) {
63+
// changed
64+
map.panTo(latLon);
65+
locomotiveMarker.setLatLng(latLon).update();
66+
}
6867
}
68+
latLonPrev[0] = latLon[0];
69+
latLonPrev[1] = latLon[1];
6970
}
7071
}
7172
}

Source/RunActivity/Viewer3D/WebServices/WebServer.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,6 @@ public ORTSApiController(Viewer viewer)
121121
Viewer = viewer;
122122
}
123123

124-
public string getLatLon()
125-
{
126-
double latitude = 0;
127-
double longitude = 0;
128-
129-
var playerLocation = Viewer.Simulator.PlayerLocomotive.WorldPosition.WorldLocation;
130-
131-
new WorldLatLon().ConvertWTC(playerLocation.TileX, playerLocation.TileZ, playerLocation.Location, ref latitude, ref longitude);;
132-
133-
string latitudeStr = (MathHelper.ToDegrees((float)latitude)).ToString("F6").Replace(',', '.');
134-
string longitudeStr = (MathHelper.ToDegrees((float)longitude)).ToString("F6").Replace(',', '.');
135-
136-
return (latitudeStr + " " + longitudeStr);
137-
}
138-
139124
#region /API/APISAMPLE
140125
public struct Embedded
141126
{
@@ -274,7 +259,7 @@ IEnumerable<string> GetValues()
274259

275260
#region /API/MAP
276261
[Route(HttpVerbs.Get, "/MAP")]
277-
public string LatLon() => getLatLon();
262+
public LatLon LatLon() => Viewer.Simulator.PlayerLocomotive.GetLatLon();
278263
#endregion
279264
}
280265
}

0 commit comments

Comments
 (0)