Topic: GPX Line car

Hi Alex I hope all is well. I upload a GPX and redraw it with DrawingElement line on the 3d map. Everything OK. Request. How can I make a GameObject ( car ) move along the line with a ui slider ? Thank you

Re: GPX Line car

Hi.

You need to calculate the total length of the route (add the lengths of all segments).
Based on the progress of your slider, the length of the route that the car has covered (total * progress).
Next, you need to find the segment (two points, AB, BC, CD, etc.) where the car is.
Go through all the segments of your route.
If the length of the segment is less than the covered length, subtract the length of the segment from the covered length.
If it is greater than or equal to, you have found the desired segment.
Next, you need to find a point on the segment.
This is very easy to do, just Lerp(a, b, coveredLength / segmentLength).

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: GPX Line car

Thanks Alex. Can you give me an example?

Re: GPX Line car

Sure, here's the code:

/// <summary>
/// Current progress value (0-1)
/// </summary>
public float progress;
    
/// <summary>
/// Points of the route
/// </summary>
private OnlineMapsVector2d[] points;

private void UpdateMarkerPosition()
{
    // Calculate the total distance of the route.
    double totalDistanceKm = 0;
    for (int i = 0; i < points.Length - 1; i++)
    {
        OnlineMapsVector2d p1 = points[i];
        OnlineMapsVector2d p2 = points[i + 1];
        totalDistanceKm += OnlineMapsUtils.DistanceBetweenPoints(p1.x, p1.y, 0, p2.x, p2.y, 0);
    }
    
    // Calculate the distance covered.
    double coveredDistanceKm = totalDistanceKm * Mathf.Clamp01(progress);

    for (int i = 0; i < points.Length - 1; i++)
    {
        // Calculate the distance between the current and next point.
        OnlineMapsVector2d p1 = points[i];
        OnlineMapsVector2d p2 = points[i + 1];
        double distance = OnlineMapsUtils.DistanceBetweenPoints(p1.x, p1.y, 0, p2.x, p2.y, 0);
        
        // If the distance covered is greater than the distance between the current and next point,
        // subtract the distance between the current and next point from the distance covered.
        if (coveredDistanceKm > distance)
        {
            coveredDistanceKm -= distance;
        }
        // If the distance covered is less or equal than the distance between the current and next point,
        // calculate the position of the marker.
        else
        {
            double lng = p1.x + (p2.x - p1.x) * coveredDistanceKm / distance;
            double lat = p1.y + (p2.y - p1.y) * coveredDistanceKm / distance;
            marker.SetPosition(lng, lat);
            break;
        }
    }
}

P.S. This is theoretical code and I have not tested it. I hope I wrote it without bugs.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: GPX Line car

Thanks Alex.

Re: GPX Line car

Thanks Alex for the example. I can get the distance with the slider value between two coordinates that I use as a test but how can I add an indicator ( game object ) that shows the position of the slider value on the map line (line render gpx) ?

Re: GPX Line car

The code above does what you are asked about.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.