Skip to content

Commit

Permalink
Created class LatLon. Created function GetLatLon in TrainCar. Reworke…
Browse files Browse the repository at this point in the history
…d index.js. Returned LatLon object for /API/MAP.
  • Loading branch information
sweiland-openrails committed Nov 22, 2022
1 parent 7dab270 commit db5764c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 37 deletions.
18 changes: 18 additions & 0 deletions Source/Orts.Simulation/Common/WorldLatLon.cs
Expand Up @@ -287,4 +287,22 @@ static double Adjust_Lon(double value)
}

}

/// <summary>
/// Class to store the latitude and longitude values of a point on the map
/// </summary>
public class LatLon
{
private readonly float _lat;
private readonly float _lon;

public LatLon(float lat, float lon)
{
this._lat = lat;
this._lon = lon;
}

public float Lat => _lat;
public float Lon => _lon;
}
}
20 changes: 20 additions & 0 deletions Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs
Expand Up @@ -34,6 +34,7 @@
//#define DEBUG_BRAKE_SLIDE

using Microsoft.Xna.Framework;
using Orts.Common;
using Orts.Formats.Msts;
using Orts.Parsers.Msts;
using Orts.Simulation.AIs;
Expand Down Expand Up @@ -3348,6 +3349,25 @@ public virtual void UpdateHeatLoss()

TotalCarCompartmentHeatLossW = HeatLossTransmissionW + HeatLossInfiltrationW + HeatLossVentilationW;
}

/// <summary>
/// Determine latitude/longitude position of the current TrainCar
/// </summary>
public LatLon GetLatLon()
{
double lat = 0;
double lon = 0;

var playerLocation = WorldPosition.WorldLocation;

new WorldLatLon().ConvertWTC(playerLocation.TileX, playerLocation.TileZ, playerLocation.Location, ref lat, ref lon);

LatLon latLon = new LatLon(
MathHelper.ToDegrees((float)lat),
MathHelper.ToDegrees((float)lon));

return (latLon);
}
}

public class WheelAxle : IComparer<WheelAxle>
Expand Down
43 changes: 22 additions & 21 deletions Source/RunActivity/Viewer3D/WebServices/Web/Map/index.js
Expand Up @@ -16,11 +16,11 @@
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
//

var hr = new XMLHttpRequest;
var httpCodeSuccess = 200;
var xmlHttpRequestCodeDone = 4;
const hr = new XMLHttpRequest;
const httpCodeSuccess = 200;
const xmlHttpRequestCodeDone = 4;

var locomotivMarker;
var locomotiveMarker;
var map;
var latLonPrev = [0, 0];

Expand All @@ -44,28 +44,29 @@ function ApiMap() {
hr.send();
hr.onreadystatechange = function () {
if (this.readyState == xmlHttpRequestCodeDone && this.status == httpCodeSuccess) {
var responseText = JSON.parse(hr.responseText);
if (responseText.length > 0) {
latLon = responseText.split(" ");
if (typeof locomotivMarker !== 'undefined') {
if ((latLon[0] != latLonPrev[0]) || (latLon[1] != latLonPrev[1])) {
map.panTo(latLon);
}
} else {
MapInit(latLon);
}

if ((latLon[0] != latLonPrev[0]) || (latLon[1] != latLonPrev[1])) {
if (typeof locomotivMarker !== 'undefined') {
locomotivMarker.removeFrom(map);
}
locomotivMarker = L.marker(
let latLonObj = JSON.parse(hr.responseText);

if (latLonObj != null) {

let latLon = [latLonObj.Lat, latLonObj.Lon];

if (typeof locomotiveMarker == 'undefined') {
// init
MapInit(latLon);
locomotiveMarker = L.marker(
latLon,
{ icon: myIcon }
).addTo(map);
latLonPrev[0] = latLon[0];
latLonPrev[1] = latLon[1];
} else {
if ((latLon[0] != latLonPrev[0]) || (latLon[1] != latLonPrev[1])) {
// changed
map.panTo(latLon);
locomotiveMarker.setLatLng(latLon).update();
}
}
latLonPrev[0] = latLon[0];
latLonPrev[1] = latLon[1];
}
}
}
Expand Down
17 changes: 1 addition & 16 deletions Source/RunActivity/Viewer3D/WebServices/WebServer.cs
Expand Up @@ -121,21 +121,6 @@ public ORTSApiController(Viewer viewer)
Viewer = viewer;
}

public string getLatLon()
{
double latitude = 0;
double longitude = 0;

var playerLocation = Viewer.Simulator.PlayerLocomotive.WorldPosition.WorldLocation;

new WorldLatLon().ConvertWTC(playerLocation.TileX, playerLocation.TileZ, playerLocation.Location, ref latitude, ref longitude);;

string latitudeStr = (MathHelper.ToDegrees((float)latitude)).ToString("F6").Replace(',', '.');
string longitudeStr = (MathHelper.ToDegrees((float)longitude)).ToString("F6").Replace(',', '.');

return (latitudeStr + " " + longitudeStr);
}

#region /API/APISAMPLE
public struct Embedded
{
Expand Down Expand Up @@ -274,7 +259,7 @@ IEnumerable<string> GetValues()

#region /API/MAP
[Route(HttpVerbs.Get, "/MAP")]
public string LatLon() => getLatLon();
public LatLon LatLon() => Viewer.Simulator.PlayerLocomotive.GetLatLon();
#endregion
}
}

0 comments on commit db5764c

Please sign in to comment.