Map interaction client/Track layer interaction: Difference between revisions

From Maria GDK Wiki
Jump to navigation Jump to search
()
()
Line 84: Line 84:
     private static Random _rand = new Random();
     private static Random _rand = new Random();


    /// <summary>
    /// Provides a random int, within the given limits.
    /// </summary>
    /// <param name="minimum">Minimum value</param>
    /// <param name="maximum">Maximum value</param>
    /// <returns>Requested value.</returns>
     public static int GetRandomInt(int minimum, int maximum)
     public static int GetRandomInt(int minimum, int maximum)
     {
     {
Line 95: Line 89:
     }
     }


    /// <summary>
    /// Provides a random double, within the given limits.
    /// </summary>
    /// <param name="minimum">Minimum value</param>
    /// <param name="maximum">Maximum value</param>
    /// <returns>Requested value.</returns>
     public static double GetRandomDouble(double minimum, double maximum)
     public static double GetRandomDouble(double minimum, double maximum)
     {
     {
Line 106: Line 94:
     }
     }


    /// <summary>
    /// Provides a random position, within the given geographical area.
    /// </summary>
    /// <param name="geoRect">
    /// Geographical area. (E.g.from any Maria layer (IMariaLayer.GeoContext.Viewport.GeoRect))
    /// </param>
    /// <returns>Requested position</returns>
     public static GeoPos GetRandomPosition(GeoRect geoRect, double margin = 1.0)
     public static GeoPos GetRandomPosition(GeoRect geoRect, double margin = 1.0)
     {
     {

Revision as of 16:23, 25 September 2019

Track Layer preparations

How to connect to, and display tracks from, a track service was described in MariaBasicMapClient, .

Please note that creating, updating and removing tracks is performed towards the connected track service, and will have effect for all clients connected to this service, while selection handling and track display are performed locally on your client only.

We will now perform further interactions towards the track layer through the track layer interfaces:

Interface Accesed through
IMariaTrackLayer *_trackLayer*
IMariaExtendedTrackLayer *_trackLayer.ExtendedTrackLayer*

Create tracks

Use the TrackData class to create new track objects. Apply desired track info, and pass it to the track service with the SetTrackData method.

string list = . . . // e.g. "MyList" or  _trackLayer.ActiveTrackList;
string strId = . . . // Unique Id;
ItemId id = new ItemId(list, strId);
double speed = . . .
double course = . . .
GeoPos pos = . . . 

TrackData trackData =  new TrackData(itemId, pos, course, speed) 
{ ObservationTime = DateTime.UtcNow };
trackData.SetTrackDataField("name", strId);

_trackLayer.SetTrackData(trackData);

For your convinience, here here you have methods for adding basic tracks at a random possition (RandomProvider class) within the current screen area:

public void OnAddTrack()
{
    if (_trackLayer.ActiveTrackList == null)
        return;

    var strId = "Track-" + _trackCnt++ + "_" + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
    var itemId = new ItemId(_trackLayer.ActiveTrackList, strId);
    var speed = RandomProvider.GetRandomDouble(5.0, 50.0);
    var course = RandomProvider.GetRandomDouble(0, 360.0);


    var pos = RandomProvider.GetRandomPosition(_trackLayer.GeoContext.Viewport.GeoRect);
    var trackData = new TrackData(itemId, pos, course, speed) { ObservationTime = DateTime.UtcNow };
    trackData.Fields["name"] = strId;


    switch (_trackCnt % 4)
    {
        case 0:
            trackData.Fields["symbol.2525code"] = "SUS*------*****";
            trackData.Fields["identity"] = "Undef";
            trackData.Fields["type"] = "U";
            break;
        case 1:
            trackData.Fields["symbol.2525code"] = "SFS*------*****";
            trackData.Fields["identity"] = "Friend";
            trackData.Fields["type"] = "F";
            break;
        case 2:
            trackData.Fields["symbol.2525code"] = "SHS*------*****";
            trackData.Fields["identity"] = "Hostile";
            trackData.Fields["type"] = "H";          
            break;
        case 3:
            trackData.Fields["symbol.2525code"] = "SNS*------*****";
            trackData.Fields["identity"] = "Neutral";
            trackData.Fields["type"] = "N";
            break;
    }

    _trackLayer.SetTrackData(trackData);
    _trackCnt++;
}

public static class RandomProvider
{
    private static Random _rand = new Random();

    public static int GetRandomInt(int minimum, int maximum)
    {
        return _rand.Next(minimum, maximum);
    }

    public static double GetRandomDouble(double minimum, double maximum)
    {
        return _rand.NextDouble() * (maximum - minimum) + minimum;
    }

    public static GeoPos GetRandomPosition(GeoRect geoRect, double margin = 1.0)
    {
        var rect = new GeoRect(geoRect.Center)
        {
            DeltaLat = geoRect.DeltaLat * 0.9,
            DeltaLon = geoRect.DeltaLon * 0.9
        };


        var pos = new GeoPos(
            GetRandomDouble(rect.UpperLeft.Lat, rect.LowerRight.Lat),
            GetRandomDouble(rect.UpperLeft.Lon, rect.LowerRight.Lon));

        return pos;
    }

    public static GeoPoint GetRandomGeoPoint(GeoRect geoRect, double minAlt, double maxAlt)
    {
        var pt = GetRandomPosition(geoRect);
        var alt = GetRandomDouble(minAlt, maxAlt);
        return new GeoPoint() { Latitude = pt.Lat, Longitude = pt.Lon, Altitude = alt };
    }
}
Map area with new track

Update tracks

Track information can be obtained with the track layer interface GetTrackData method, and updated by the SetTrackData method.

Example:

var dataCollection = _trackLayer.GetTrackData(arrayOfTrackIds);
foreach (var trackData in dataCollection)
{
    trackData.Pos = newPosition;
    trackData.Speed = newSpeed;
    trackData.Course = newCourse;
}

_trackLayer.SetTrackData(dataCollection);

Here you have a code example for updating position, course and speed of the currently added tracks.

public void OnUpdateTrack()
{
    var dataCollection = _trackLayer.GetTrackData();
    foreach (var track in dataCollection)
    {
        var rect = _trackLayer.GeoContext.Viewport.GeoRect;

        rect.Center = track.Pos ?? RandomProvider.GetRandomPosition(rect);
        rect.InflateRelative(0.3);

        track.Course = RandomProvider.GetRandomDouble(-179.9, 180.0);
        track.Speed = RandomProvider.GetRandomDouble(5.0, 50.0);
        track.Pos = RandomProvider.GetRandomPosition(rect);

        _trackLayer.SetTrackData(track);
    }
}
Map area with updated track

Track selection

The Maria Control supports selection of tracks by user action in the map area, single select or area select. For additional info, see section “”.

In addition, setting and getting selection state can be performed programmatically through the extended track layer interface.

Example:

var selection = _trackLayer.ExtendedTrackLayer.Selected;

_trackLayer.ExtendedTrackLayer.Deselect(itemId);
_trackLayer.ExtendedTrackLayer.Select(itemId);

_trackLayer.ExtendedTrackLayer.Select(newSelection);
_trackLayer.ExtendedTrackLayer.Selected.Clear();

The sample project contains example code for selecting and de-selecting tracks.

Track selection in map area

Remove tracks

The DeleteTrackData method in the track layer interface is used to remove tracks from the track store.

Example:

_trackLayer.DeleteTrackData(instanceId);

The sample project contains example code for removing selected tracks.

Removing tracks