1 (edited by SuperBuf 2016-07-25 17:18:05)

Topic: Correct waypoint format

What's the proper string format for waypoints in Advanced Direction search?

Example url that i'm sending out that is failing:

https://maps.google.com/maps/api/directions/xml?origin=50.89974,5.749269&destination=50.82225,5.771378&sensor=false&waypoints=optimize:true|50.89974,5.749269|50.86686,5.773848|50.8013,5.699512|50.79523,5.685507|50.78641,5.724956|50.92665,5.592241|50.92524,5.682963|50.88491,5.677073|50.80597,5.646475|50.86802,5.734172|50.89124,5.594845|50.82225,5.771378

2 (edited by SuperBuf 2016-07-25 17:42:12)

Re: Correct waypoint format

Okay, weird behaviour and no clue why, but disabling the debug.log results in a failed path. Reenabling it makes it work..

for(int i = 0; i < selectedRoute.Count; i++){
            Debug.Log(selectedRoute[i].position.x +" "+ selectedRoute[i].position.y );
            wayPoints[i] = selectedRoute[i].position.y +","+ selectedRoute[i].position.x;
        }

It seems position is not initialised until the first call??

Re: Correct waypoint format

Hello.

What is «selectedRoute» and «selectedRoute.position»?
selectedRoute - is some List, right?
List of what? OnlineMapsFindDirectionResult does not contain «position».

Kind Regards,
Infinity Code Team.

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

Re: Correct waypoint format

Hi,

Yeah sorry, should clarify. It's a list of OnlineMapsMarkers. OnlineMapsFindDirectionAdvanced requires a string[] to be sent across. I imagine this is a lat,long string. Hence the position. What else should i send a long? I want to plot a path between markers.

Re: Correct waypoint format

have since revised the code a little.

void OnGUI(){
        if(GUILayout.Button("GENERATE")){
            List<OnlineMapsMarker> selectedRoute = categorisedMarkers[GPSCategory.POPULAR];
            string[] wayPoints = new string[selectedRoute.Count];
            for(int i = 0; i < selectedRoute.Count; i++){
                double lat, lng;
                OnlineMaps.instance.GetPosition(out lng, out lat );
                wayPoints[i] = lat +","+ lng;
            }
            OnlineMapsGoogleAPIQuery query = OnlineMapsFindDirectionAdvanced.Find( selectedRoute[0].position, selectedRoute[selectedRoute.Count-1].position, OnlineMapsFindDirectionMode.bicycling, wayPoints);
            query.OnComplete += OnFindDirectionComplete;
        }
    }

Seems to get paths now (8 waypoint limit is good to know btw). The order of variables is opposite of what your api dictates. Google takes LatLng not LngLat. So i switched them around in a ',' string it and seems to work though it completely ignores the waypoints and simply goes from start to end. Even with optimize=true ripped out of the url.

I have a few weird bugs which i think are related to race conditions. Is there a certain flow of events i should implement before using the map to make sure it's all loaded?

Re: Correct waypoint format

Hello.

I just checked it. Waypoints are used. Test the code below.
In Online Maps v2.4 we have added the ability to pass parameters using the object.
I think this way is much more readable, extensible, and solves the problem of who is first (latitude and longitude), because you can use object[], where each item Vector2 or string.

The order of variables is opposite of what your api dictates. Google takes LatLng not LngLat.

Long time ago (in the first versions of Online Maps v1.x), we used the geographical way (latitude first).
This caused a ton of problems for users because it conflict with another rule: X first. X - is a longitude.
So now in Online Maps API everywhere X (or lng) first.

8 waypoint limit is good to know btw

It is not described in Google Directions API documentation, and I do not know about it.
We will add it in description of the method / variable.

I have a few weird bugs which i think are related to race conditions. Is there a certain flow of events i should implement before using the map to make sure it's all loaded?

Unfortunately, I did not understand your question.
Please rephrase it.

Test of FindDirectionAdvanced with waypoint (Online Maps v2.4 way):

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

public class TestFindDirectionWithWaypoints:MonoBehaviour
{
    private void OnFindDirectionComplete(string response)
    {
        OnlineMapsFindDirectionResult result = OnlineMapsFindDirectionAdvanced.GetResult(response);
        if (result != null)
        {
            Debug.Log(result.routes.Length);
            foreach (OnlineMapsFindDirectionResult.Route route in result.routes)
            {
                OnlineMaps.instance.AddDrawingElement(
                    new OnlineMapsDrawingLine(route.overview_polyline.ToList(), Color.red, 3)
                );
            }
        }
        else
        {
            Debug.Log(response);
        }
    }

    private void OnGUI()
    {
        if (GUILayout.Button("Test"))
        {
            OnlineMapsMarker[] markers = OnlineMaps.instance.markers;
            if (markers.Length < 3) return;

            Vector2 origin = markers[0].position;
            Vector2 destination = markers[markers.Length - 1].position;

            List<Vector2> waypoints = new List<Vector2>();

            for (int i = 1; i < markers.Length - 1; i++) waypoints.Add(markers[i].position);

            OnlineMapsFindDirectionAdvanced.Find(
                new OnlineMapsFindDirectionAdvanced.Params(origin, destination)
                {
                    waypoints = waypoints.Cast<object>().ToArray(),
                    alternatives = true,
                    mode = OnlineMapsFindDirectionMode.bicycling
                }
                ).OnComplete += OnFindDirectionComplete;

            OnlineMaps.instance.RemoveAllDrawingElements();
        }
    }

    private void OnMapClick()
    {
        Vector2 coords = OnlineMapsControlBase.instance.GetCoords();
        OnlineMaps.instance.AddMarker(coords, "Marker" + (OnlineMaps.instance.markers.Length + 1));
    }

    private void Start()
    {
        OnlineMapsControlBase.instance.OnMapClick += OnMapClick;
    }
}
Kind Regards,
Infinity Code Team.

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