1 (edited by mikejm 2021-12-05 10:13:33)

Topic: Online Maps only draws the line for route in app, not Unity Editor

I have got my Google API key working and I can now from the Atlas examples get Autocomplete queries back successfully and also get routes return from:

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

I can enter two addresses as strings, and the returned data is correct in terms of distance and expected duration of transit. So I know the query is going and coming back okay.

However, it is not drawing the red line on the map for the route between the two locations in Unity Editor. It does seem to draw it in the real Android app. Is there any obvious reason this would be the case?

I am running a script on my Maps object with:

Start(){
        FindRouteRequest("Street Address 1, City", "Street Address 2, City");

}


    private void FindRouteRequest(Vector2 source, string destination) {

        // Find route using Google Directions API
        OnlineMapsGoogleDirections request = new OnlineMapsGoogleDirections(googleAPIKey, source, destination);
        request.OnComplete += OnGoogleDirectionsComplete;
        request.Send();

    }
private void OnGoogleDirectionsComplete(string response) {
        Debug.Log(response);

        // Try load result
        OnlineMapsGoogleDirectionsResult result = OnlineMapsGoogleDirections.GetResult(response);
        if (result == null || result.routes.Length == 0) return;

        // Get the first route
        OnlineMapsGoogleDirectionsResult.Route route = result.routes[0];

        // Draw route on the map
        OnlineMapsDrawingElementManager.AddItem(new OnlineMapsDrawingLine(route.overview_polyline, Color.red, 5));

        //zoom to fit route

        // Calculate the distance
        int distance = route.legs.Sum(l => l.distance.value); // meters

        // Calculate the duration
        int duration = route.legs.Sum(l => l.duration.value); // seconds

        // Log distane and duration
        Debug.Log("Distance: " + distance + " meters, or " + (distance / 1000f).ToString("F2") + " km");
        Debug.Log("Duration: " + duration + " sec, or " + (duration / 60f).ToString("F1") + " min, or " + (duration / 3600f).ToString("F1") + " hours");
    }

As said, all the Debug output seems 100% correct so it seems to be working, and it does draw the route in Android app, just not in Unity Editor.

Is there any good reason this would be the case?

Is there something else I need to do to see the line in Unity Editor?

Also, is there any good way to get the map to zoom to the size of the line? Ie. To fit the route to screen?

Thanks.

Re: Online Maps only draws the line for route in app, not Unity Editor

Hello.

I have never seen a line not drawn in the editor, but drawn on the device, and no one has reported a similar problem before.
Most likely there is something wrong with the shaders and drawing order.
But what exactly is difficult to say.
If you send me your project by email (support@infinity-code.com), I can look and tell you what is wrong.

Use OnlineMapsGoogleDirectionsResult.Route.bounds to get the bounds of the route, and then use OnlineMapsUtils.GetCenterPointAndZoom to find the center and best zoom for your route.
https://infinity-code.com/doxygen/onlin … 5bb9eef016
https://infinity-code.com/doxygen/onlin … 244fb67413

Kind Regards,
Infinity Code Team.

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

Re: Online Maps only draws the line for route in app, not Unity Editor

Alex Vertax wrote:

Hello.

I have never seen a line not drawn in the editor, but drawn on the device, and no one has reported a similar problem before.
Most likely there is something wrong with the shaders and drawing order.
But what exactly is difficult to say.
If you send me your project by email (support@infinity-code.com), I can look and tell you what is wrong.

Use OnlineMapsGoogleDirectionsResult.Route.bounds to get the bounds of the route, and then use OnlineMapsUtils.GetCenterPointAndZoom to find the center and best zoom for your route.

Thanks. I figured out what the error was. It happens when you make a map and duplicate it so there are two copies in the scene. Ie. If you make a map, then store it as a prefab, and instantiate from it, and you have the original plus the duplicate in the scene together it creates funny drawing glitches.

You can replicate it by adding a 2D RawImage Terrain Google Maps, setting the zoom appropriately, giving an API key, and then attaching the script I posted. This will work fine. But if you then right click on the Map game object and hit "Duplicate" then space them out so you can see them both, the drawing gets weird. It will for me flash the route on both maps then it disappears from both.

In my project, I was testing with the original prefab open and instantiating from it. What was happening was the instantiated copy was getting rendered correctly but the original prefab wasn't. When I'd open the instantiated copy to see it from the menu it was hiding behind, then the original prefab would also get updated with the matching route.

So it's just an issue from having multiple copies of the same map in one scene. Maybe from sharing the same texture? It doesn't know which map copy to draw on?

Either way, I can work around it as I don't need two copies of the same map in one scene. I just didn't see it in my Android build as obviously there I disabled the original prefab object.

Do you think there is anything to "fix" on your end? Either way I am okay but it would be nice if both copies got drawn on in such a circumstance.

I will try your zooming script suggestions next. Thanks again.

Re: Online Maps only draws the line for route in app, not Unity Editor

In this case, there is a conflict between drawing two maps to the same texture, when only one instance of the map has a route for drawing.
It doesn't need fixing on my side.
When using multiple maps, you need to use non-static methods to interact with the map, otherwise it will only affect the last instance of the map.

Kind Regards,
Infinity Code Team.

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