1

For a little demo I'm building, I'm trying to run Google Maps navigation right out of a Unity3D app, by just opening an URL such as Application.OpenURL( "https://www.google.com/maps?daddr=Some+Address" );. While this causes the Google Maps app to open correctly, giving me an overview of possible routes, I'd like to skip this and start navigation right away, say, using the shortest route (or any route, it really doesn't matter). I figured this can usually be accomplished with Intents, but it seems like Unity does not exhibit an interface for this.

Is there some other way to accomplish this?

iko79
  • 1,127
  • 10
  • 23

1 Answers1

1

Since version 2018.3 Unity allows you to add .java files into your Unity Project and they are compiled when you build an Android player.

1) Copy the output UnityAndroidLocation.java file into the Assets/Plugins/Android directory in your Unity project.

2) Go to: [File] > [build Settings] > [Player Settings] > [Player] and copy package name.

3) In both classes find and replace "ual.ual.ual" package name with that you copied in step 2).


"UnityAndroidLocation.cs" class:

using UnityEngine;
public class UnityAndroidLocation : MonoBehaviour
{
#if UNITY_ANDROID || UNITY_EDITOR
    private static AndroidJavaObject plugin = null;
#endif


    // Use this for initialization
    void Awake()
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        plugin = new AndroidJavaClass("ual.ual.ual.UnityAndroidLocation").CallStatic<AndroidJavaObject>("instance");
#endif
    }


    /// <summary>
    /// <para>Open Google Map with Specific Adress.</para>
    /// <para>Adress: [City, street]</para>
    /// </summary>
    /// <param name="Adress">Adress.</param>
    public void GoogleMapWithAdress(string Adress)
    {
        plugin.Call("GoogleMapWithAdress", Adress);
    }

    /// <summary>
    /// <para>Open Google Map with Specific Coordinate.</para>
    /// <para>Zoom (integer number 2-23)</para>
    /// </summary>
    /// <param name="latitude">Latitude.</param>
    /// <param name="longitude">Longitude.</param>
    /// <param name="zoom">Zoom.</param>
    public void GoogleMapWithCoordinate(float latitude, float longitude, int zoom)
    {
        plugin.Call("GoogleMapWithCoordinate", latitude, longitude, zoom);
    }
}

"UnityAndroidLocation.java" class:

package ual.ual.ual;

import android.content.Intent;
import android.net.Uri;
import com.unity3d.player.UnityPlayer;

public class UnityAndroidLocation {
    private static UnityAndroidLocation _instance;

    public UnityAndroidLocation() {
    }

    public void GoogleMapWithAdress(String Adress) {
        String uri = "geo:0,0?q=" + Adress;
        Intent mapIntent = new Intent("android.intent.action.VIEW", Uri.parse(uri));
        UnityPlayer.currentActivity.startActivity(mapIntent);
    }

    public void GoogleMapWithCoordinate(float latitude, float longitude, int zoom) {
        if (zoom < 2) {
            zoom = 2;
        }

        if (zoom > 23) {
            zoom = 23;
        }

        String uri = "geo:" + latitude + "," + longitude + "?z=" + zoom;
        Intent mapIntent = new Intent("android.intent.action.VIEW", Uri.parse(uri));
        UnityPlayer.currentActivity.startActivity(mapIntent);
    }

    public static UnityAndroidLocation instance() {
        if (_instance == null) {
            _instance = new UnityAndroidLocation();
        }

        return _instance;
    }
}

Note: for example if your package name is "com.my.app" the resulting name would be:

"UnityAndroidLocation.cs" class:

plugin = new AndroidJavaClass("com.my.app.UnityAndroidLocation").CallStatic<AndroidJavaObject>("instance");

"UnityAndroidLocation.java" class:

package com.my.app;

import android.content.Intent;
import android.net.Uri;
import com.unity3d.player.UnityPlayer;
Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48