diff --git a/Source/Orts.Simulation/Common/WorldLatLon.cs b/Source/Orts.Simulation/Common/WorldLatLon.cs index df1b174b38..b97dda1558 100644 --- a/Source/Orts.Simulation/Common/WorldLatLon.cs +++ b/Source/Orts.Simulation/Common/WorldLatLon.cs @@ -45,8 +45,8 @@ public class WorldLatLon // The upper left corner of the Goode projection is ul_x,ul_y // The bottom right corner of the Goode projection is -ul_x,-ul_y - int ul_x = -20015000; // -180 deg in Goode projection - int ul_y = 8673000; // +90 deg lat in Goode projection + int ul_x = -20013965; // -180 deg in Goode projection + int ul_y = 8674008; // +90 deg lat in Goode projection // Offsets to convert Goode raster coordinates to MSTS world tile coordinates int wt_ew_offset = -16385; @@ -287,4 +287,22 @@ static double Adjust_Lon(double value) } } + + /// + /// Class to store the latitude and longitude values of a point on the map + /// + 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; + } } diff --git a/Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs b/Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs index e754611329..29c49c804e 100644 --- a/Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs +++ b/Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs @@ -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; @@ -3348,6 +3349,25 @@ public virtual void UpdateHeatLoss() TotalCarCompartmentHeatLossW = HeatLossTransmissionW + HeatLossInfiltrationW + HeatLossVentilationW; } + + /// + /// Determine latitude/longitude position of the current TrainCar + /// + 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 diff --git a/Source/RunActivity/Viewer3D/WebServices/Web/Map/index.html b/Source/RunActivity/Viewer3D/WebServices/Web/Map/index.html new file mode 100644 index 0000000000..242e652bc6 --- /dev/null +++ b/Source/RunActivity/Viewer3D/WebServices/Web/Map/index.html @@ -0,0 +1,58 @@ + + + + + OR: Map + + + + + + + + + + + + +
+ + + + diff --git a/Source/RunActivity/Viewer3D/WebServices/Web/Map/index.js b/Source/RunActivity/Viewer3D/WebServices/Web/Map/index.js new file mode 100644 index 0000000000..a9aca09484 --- /dev/null +++ b/Source/RunActivity/Viewer3D/WebServices/Web/Map/index.js @@ -0,0 +1,79 @@ +// COPYRIGHT 2009, 2010, 2011, 2012, 2013, 2014 by the Open Rails project. +// +// This file is part of Open Rails. +// +// Open Rails is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Open Rails is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Open Rails. If not, see . +// + +const hr = new XMLHttpRequest; +const httpCodeSuccess = 200; +const xmlHttpRequestCodeDone = 4; + +var locomotiveMarker; +var map; +var latLonPrev = [0, 0]; + +function MapInit(latLon) { + + map = L.map('map').setView(latLon, 13); + L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { + maxZoom: 19, + attribution: 'Map data: © OpenStreetMap contributors' + }).addTo(map); + + L.tileLayer('https://{s}.tiles.openrailwaymap.org/standard/{z}/{x}/{y}.png', { + maxZoom: 19, + attribution: ' | Map style: © OpenRailwayMap (CC-BY-SA)' + }).addTo(map); +} + +function ApiMap() { + + hr.open("GET", "/API/MAP/", true); + hr.send(); + hr.onreadystatechange = function () { + if (this.readyState == xmlHttpRequestCodeDone && this.status == httpCodeSuccess) { + + 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); + } 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]; + } + } + } +} + +var myIcon = L.icon({ + iconUrl: 'locomotive.png', + iconSize: [29, 24], + iconAnchor: [9, 21], +}) diff --git a/Source/RunActivity/Viewer3D/WebServices/Web/Map/locomotive.png b/Source/RunActivity/Viewer3D/WebServices/Web/Map/locomotive.png new file mode 100644 index 0000000000..06a5693d12 Binary files /dev/null and b/Source/RunActivity/Viewer3D/WebServices/Web/Map/locomotive.png differ diff --git a/Source/RunActivity/Viewer3D/WebServices/Web/index.html b/Source/RunActivity/Viewer3D/WebServices/Web/index.html index 8df906846c..9c0e279eda 100644 --- a/Source/RunActivity/Viewer3D/WebServices/Web/index.html +++ b/Source/RunActivity/Viewer3D/WebServices/Web/index.html @@ -37,6 +37,7 @@

Open Rails - Web Interface

  • Head Up Display (Alt+F5)
  • Cab Controls
  • Time
  • +
  • Map
  • API Sample
  • diff --git a/Source/RunActivity/Viewer3D/WebServices/WebServer.cs b/Source/RunActivity/Viewer3D/WebServices/WebServer.cs index 37d7cb3838..decf300642 100644 --- a/Source/RunActivity/Viewer3D/WebServices/WebServer.cs +++ b/Source/RunActivity/Viewer3D/WebServices/WebServer.cs @@ -26,8 +26,10 @@ using Microsoft.Xna.Framework; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; +using Orts.Common; using Orts.Simulation.Physics; using Orts.Viewer3D.RollingStock; +using ORTS.Common; using System; using System.Collections.Generic; using System.Linq; @@ -112,13 +114,13 @@ internal class ORTSApiController : WebApiController /// The Viewer to serve train data from. /// private readonly Viewer Viewer; + protected WorldLocation cameraLocation = new WorldLocation(); public ORTSApiController(Viewer viewer) { Viewer = viewer; } - #region /API/APISAMPLE public struct Embedded { @@ -254,5 +256,10 @@ IEnumerable GetValues() [Route(HttpVerbs.Get, "/TIME")] public double Time() => Viewer.Simulator.ClockTime; #endregion + + #region /API/MAP + [Route(HttpVerbs.Get, "/MAP")] + public LatLon LatLon() => Viewer.Simulator.PlayerLocomotive.GetLatLon(); + #endregion } -} \ No newline at end of file +}