1 (edited by cilkamib 2021-03-22 19:50:12)

Topic: Simple gps navigator in the mobile application (Unity)

Good day. I would like to create a Gps navigator for a mobile application with a route starting from a point of a certain location to the end point of the created markers and so that as the user moves, the paved line is erased as the route traveled (as in the Google maps application). Is it possible to do this with OnlineMaps? What is the best way to implement this? Thanks. While slowly studying the asset.

Re: Simple gps navigator in the mobile application (Unity)

Hello.

Something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace InfinityCode.OnlineMapsExamples
{
    public class MoveMarkerOnRouteExample2 : MonoBehaviour
    {
        /// <summary>
        /// Start location name
        /// </summary>
        public string fromPlace = "Los Angeles";

        /// <summary>
        /// End location name
        /// </summary>
        public string toPlace = "Hollywood";

        /// <summary>
        /// Speed of movement (km/h).
        /// </summary>
        public float speed = 60;

        /// <summary>
        /// Move map to marker position
        /// </summary>
        public bool lookToMarker = false;

        /// <summary>
        /// Orient marker on next point.
        /// </summary>
        public bool orientMarkerOnNextPoint = false;

        /// <summary>
        /// Reference to marker
        /// </summary>
        private OnlineMapsMarker marker;

        /// <summary>
        /// Array of route points
        /// </summary>
        private OnlineMapsVector2d[] points;

        /// <summary>
        /// Current point index
        /// </summary>
        private int pointIndex = -1;

        /// <summary>
        /// Current step progress
        /// </summary>
        private double progress;

        private List<OnlineMapsVector2d> linePoints;

        private void Start()
        {
            // Looking for a route between locations.
            OnlineMapsGoogleDirections request = OnlineMapsGoogleDirections.Find(new OnlineMapsGoogleDirections.Params(fromPlace, toPlace));
            request.OnComplete += OnComplete;
        }

        private void OnComplete(string response)
        {
            Debug.Log("OnComplete");

            OnlineMapsGoogleDirectionsResult result = OnlineMapsGoogleDirections.GetResult(response);
            if (result == null || result.routes.Length == 0)
            {
                Debug.Log("Something wrong");
                Debug.Log(response);
                return;
            }

            OnlineMapsGoogleDirectionsResult.Route firstRoute = result.routes[0];
            List<OnlineMapsGoogleDirectionsResult.Step> steps = firstRoute.legs.SelectMany(l => l.steps).ToList();

            // Create a new marker in first point.
            marker = OnlineMapsMarkerManager.CreateItem(steps[0].start_location, "Car");

            // Gets points of route.
            points = steps.SelectMany(s => s.polylineD).ToArray();

            // Create a list of points that will be drawn and that will change when following a route
            linePoints = points.ToList();

            // Draw the route.
            OnlineMapsDrawingLine route = new OnlineMapsDrawingLine(linePoints, Color.red, 3);
            OnlineMapsDrawingElementManager.AddItem(route);

            pointIndex = 0;
        }

        private void Update()
        {
            if (pointIndex == -1) return;

            // Start point
            OnlineMapsVector2d p1 = points[pointIndex];

            // End point
            OnlineMapsVector2d p2 = points[pointIndex + 1];

            // Total step distance
            double dx, dy;
            OnlineMapsUtils.DistanceBetweenPoints(p1.x, p1.y, p2.x, p2.y, out dx, out dy);
            double stepDistance = Math.Sqrt(dx * dx + dy * dy);

            // Total step time
            double totalTime = stepDistance / speed * 3600;

            // Current step progress
            progress += Time.deltaTime / totalTime;

            OnlineMapsVector2d position;

            if (progress < 1)
            {
                position = OnlineMapsVector2d.Lerp(p1, p2, progress);
                marker.SetPosition(position.x, position.y);

                // Updating the first point of the drawn line
                linePoints[0] = position;

                // Orient marker
                if (orientMarkerOnNextPoint) marker.rotation = 1.25f - OnlineMapsUtils.Angle2D((Vector2)p1, (Vector2)p2) / 360f;
            }
            else
            {
                position = p2;
                marker.SetPosition(position.x, position.y);

                // Remove the first point of the drawn line
                linePoints.RemoveAt(0);
                pointIndex++;
                progress = 0;
                if (pointIndex >= points.Length - 1)
                {
                    Debug.Log("Finish");
                    pointIndex = -1;
                }
                else
                {
                    // Orient marker
                    if (orientMarkerOnNextPoint) marker.rotation = 1.25f - OnlineMapsUtils.Angle2D(p2, points[pointIndex + 1]) / 360;
                }
            }

            if (lookToMarker) OnlineMaps.instance.SetPosition(position.x, position.y);
            OnlineMaps.instance.Redraw();
        }
    }
}
Kind Regards,
Infinity Code Team.

uContext is Editor PowerPack (more than 50 tools) that take your workflow in Unity Editor to a next level, simplifies working with content, adds new features, corrects and improves the editor’s built-in behaviors. Click here to download uContext Basic for free.

3 (edited by ashh21411 2021-07-09 15:40:58)

Re: Simple gps navigator in the mobile application (Unity)

can from place can be replaced by user gps location? how to implement getting user location position dynamically via gps inside this script

Re: Simple gps navigator in the mobile application (Unity)

Example:
https://infinity-code.com/atlas/online- … ample.html

Kind Regards,
Infinity Code Team.

uContext is Editor PowerPack (more than 50 tools) that take your workflow in Unity Editor to a next level, simplifies working with content, adds new features, corrects and improves the editor’s built-in behaviors. Click here to download uContext Basic for free.

Re: Simple gps navigator in the mobile application (Unity)

i was using move marker on route example script.



        private void Start()
        {
            if (string.IsNullOrEmpty(googleAPIKey)) Debug.LogWarning("Please specify Google API Key");

            // Looking for a route between locations.
            OnlineMapsGoogleDirections request = new OnlineMapsGoogleDirections(googleAPIKey, new Vector2(83.268809f, 17.655391f), new Vector2(83.316583f, 17.710500f));
            request.OnComplete += OnComplete;
            request.Send();
        }


in this function
OnlineMapsGoogleDirections request = new OnlineMapsGoogleDirections(googleAPIKey, new Vector2(83.268809f, 17.655391f), new Vector2(83.316583f, 17.710500f));

i want to replace new vector2 coordinates with Gps location of user, how to get gps user location lat nd long co-ordinates and pass it on here.

Re: Simple gps navigator in the mobile application (Unity)

Add Location Service component, use OnlineMapsLocationServiceBase.position to get GPS location, and use this location in request.
https://infinity-code.com/doxygen/onlin … 444f547053

Kind Regards,
Infinity Code Team.

uContext is Editor PowerPack (more than 50 tools) that take your workflow in Unity Editor to a next level, simplifies working with content, adds new features, corrects and improves the editor’s built-in behaviors. Click here to download uContext Basic for free.

7 (edited by ashh21411 2021-07-12 12:55:29)

Re: Simple gps navigator in the mobile application (Unity)

i was an beginner to c#, how to Use OnLocationInited event, to determine the initialization of GPS. could you please provide an example code which may be helpful us



     public OnlineMapsLocationServiceBase testscript;

        public Vector2 pos;


        private void Start()
        {


             testscript.position = pos;

              // Subscribe to the change location event.



            if (string.IsNullOrEmpty(googleAPIKey)) Debug.LogWarning("Please specify Google API Key");

            // Looking for a route between locations.
            OnlineMapsGoogleDirections request = new OnlineMapsGoogleDirections(googleAPIKey, pos, new Vector2(83.316583f, 17.710500f));
            request.OnComplete += OnComplete;
            request.Send();
        }


this is what i have wrote it was saying something wrong in log

Re: Simple gps navigator in the mobile application (Unity)

Example:

using UnityEngine;

public class DirectionsFromLocation : MonoBehaviour
{
    public string googleAPIKey;
    public Vector2 destination = new Vector2(83.316583f, 17.710500f);

    private void Start()
    {
        if (string.IsNullOrEmpty(googleAPIKey)) Debug.LogWarning("Please specify Google API Key");

        OnlineMapsLocationService.instance.OnLocationInited += OnLocationInited;
    }

    private void OnLocationInited()
    {
        OnlineMapsGoogleDirections request = new OnlineMapsGoogleDirections(
            googleAPIKey, 
            OnlineMapsLocationService.instance.position, 
            destination);
        request.OnComplete += OnRequestComplete;
        request.Send();
    }

    private void OnRequestComplete(string response)
    {
        // Do something
    }
}
Kind Regards,
Infinity Code Team.

uContext is Editor PowerPack (more than 50 tools) that take your workflow in Unity Editor to a next level, simplifies working with content, adds new features, corrects and improves the editor’s built-in behaviors. Click here to download uContext Basic for free.

Re: Simple gps navigator in the mobile application (Unity)

Thank You So Much. my last question in car drive by route example
1) i need my car location to my gps location but the script which was there in that scene, the lat and long coordinated were derived from online maps where we enter our default lat and long points. so it was starting from the default point which we enter. though location service component is there. its not taking gps
2) my car moment should be the user moment. but by default its moving with our desired speed which we mentioned.

thanks a lot for your support.

Re: Simple gps navigator in the mobile application (Unity)

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Collections;
using UnityEngine.Android;
using UnityEngine.UI;

public class loc : MonoBehaviour
{
    public bool lookToMarker = false;

    /// <summary>
    /// Orient marker on next point.
    /// </summary>
    public bool orientMarkerOnNextPoint = false;

    /// <summary>
    /// Reference to marker
    /// </summary>
    private OnlineMapsMarker marker;

    /// <summary>
    /// Array of route points
    /// </summary>
    private OnlineMapsVector2d[] points;

    /// <summary>
    /// Current point index
    /// </summary>
    private int pointIndex = -1;

    /// <summary>
    /// Current step progress
    /// </summary>
    private double progress;
    public float speed = 60;



    private OnlineMapsMarker playerMarker;
    public string googleAPIKey;
    public Vector2 destination = new Vector2(83.316583f, 17.710500f);

    private void Start()
    {
        if (string.IsNullOrEmpty(googleAPIKey)) Debug.LogWarning("Please specify Google API Key");

        OnlineMapsLocationService.instance.OnLocationInited += OnLocationInited;
    }

    private void OnLocationInited()
    {
        OnlineMapsGoogleDirections request = new OnlineMapsGoogleDirections(
            googleAPIKey,
            OnlineMapsLocationService.instance.position,
            destination);
        request.OnComplete += OnRequestComplete;
        request.Send();
    }

    private void OnRequestComplete(string response)
    {
        Debug.Log("OnComplete");

        OnlineMapsGoogleDirectionsResult result = OnlineMapsGoogleDirections.GetResult(response);
        if (result == null || result.routes.Length == 0)
        {
            Debug.Log("Something wrong");
            Debug.Log(response);
            return;
        }

        OnlineMapsGoogleDirectionsResult.Route firstRoute = result.routes[0];
        List<OnlineMapsGoogleDirectionsResult.Step> steps = firstRoute.legs.SelectMany(l => l.steps).ToList();

        // Create a new marker in first point.
        marker = OnlineMapsMarkerManager.CreateItem(steps[0].start_location, "Car");

        // Gets points of route.
        points = firstRoute.overview_polylineD;

        // Draw the route.
        OnlineMapsDrawingLine route = new OnlineMapsDrawingLine(points, Color.red, 3);
        OnlineMapsDrawingElementManager.AddItem(route);

        pointIndex = 0;
    }


    private void Update()
    {






        //////////////////////////////
        if (pointIndex == -1) return;

        // Start point
        OnlineMapsVector2d p1 = points[pointIndex];

        // End point
        OnlineMapsVector2d p2 = points[pointIndex + 1];

        // Total step distance
        double dx, dy;
        OnlineMapsUtils.DistanceBetweenPoints(p1.x, p1.y, p2.x, p2.y, out dx, out dy);
        double stepDistance = Math.Sqrt(dx * dx + dy * dy);

        // Total step time
        double totalTime = stepDistance / speed * 3600;

        // Current step progress
        progress += Time.deltaTime / totalTime;

        OnlineMapsVector2d position;

        if (progress < 1)
        {
            position = OnlineMapsVector2d.Lerp(p1, p2, progress);
            marker.SetPosition(position.x, position.y);

            // Orient marker
            if (orientMarkerOnNextPoint) marker.rotation = 1.25f - OnlineMapsUtils.Angle2D((Vector2)p1, (Vector2)p2) / 360f;
        }
        else
        {
            position = p2;
            marker.SetPosition(position.x, position.y);
            pointIndex++;
            progress = 0;
            if (pointIndex >= points.Length - 1)
            {
                Debug.Log("Finish");
                pointIndex = -1;
            }
            else
            {
                // Orient marker
                if (orientMarkerOnNextPoint) marker.rotation = 1.25f - OnlineMapsUtils.Angle2D(p2, points[pointIndex + 1]) / 360;
            }
        }

        if (lookToMarker) OnlineMaps.instance.SetPosition(position.x, position.y);
        OnlineMaps.instance.Redraw();
    }


}



while using the script , in the location services were on , when i deploy on my android device it was not showing any route empty map, but when i turned on find on find location my id it works but the location is 600km far from my my location.