Search code examples
apiunity-game-enginegoogle-places-api

google places api for unity3d


Wish you a Very Happy New Year!!

Has anyone used Google Places API for Unity3D? I am trying to make an augmented reality app using Places API in Unity3D. I followed all the steps mentioned here

I have first fetched the device coordinates and fed those lat-lon to the places api url for a JSON response (creating an API key for Android). Following is my Unity3D C# code,

#define ANDROID
using UnityEngine;
using System.Collections;
using System.Timers;
using SimpleJSON;

public class Places : MonoBehaviour
{
    static int Neversleep;
    LocationInfo currentGPSPosition;
    string gpsString;
    public GUIStyle locStyle;
    int wait;
    float radarRadius;
    string radarType, APIkey, radarSensor;
    string googleRespStr;

    void Start ()
    {
       Screen.sleepTimeout = SleepTimeout.NeverSleep;
       radarRadius = 1000f;
       radarType = "restaurant";
       APIkey = "MyAPIkey";
       radarSensor = "false";
    }

    void RetrieveGPSData()
    {
        currentGPSPosition = Input.location.lastData;
       gpsString = "Lat: " + currentGPSPosition.latitude + "  Lon: " + currentGPSPosition.longitude + "  Alt: " + currentGPSPosition.altitude + 
         "  HorAcc: " + Input.location.lastData.horizontalAccuracy + "  VerAcc: " + Input.location.lastData.verticalAccuracy + "  TS: " + Input.location.lastData.timestamp;
    }

    void OnGUI ()
    {
       GUI.Box (ScaleRect.scaledRect(25,25,700,100), "");
       GUI.Label (ScaleRect.scaledRect(35,25,700,100), gpsString, locStyle);

       GUI.Box (ScaleRect.scaledRect(25,130,700,800), "");
       GUI.Label (ScaleRect.scaledRect(35,135,700,800), "" +googleRespStr, locStyle);

#if PC
       Debug.Log("On PC / Don't have GPS");
#elif !PC
        Input.location.Start(10f,1f);
       int wait = 1000;

        if(Input.location.isEnabledByUser)
        {
            while(Input.location.status == LocationServiceStatus.Initializing && wait>0)
            {
                wait--;
            }
            if (Input.location.status == LocationServiceStatus.Failed)
         {}

            else
         {
          RetrieveGPSData();
          StartCoroutine(Radar());
            }
        }
        else
        {
            GameObject.Find("gps_debug_text").guiText.text = "GPS not available";
        }
#endif   
    }

    IEnumerator Radar ()
    {
       string radarURL = "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=" + currentGPSPosition.latitude + "," + currentGPSPosition.longitude + "&radius=" + radarRadius + "&types=" + radarType + "&sensor=false" + radarSensor + "&key=" + APIkey;
        WWW googleResp = new WWW(radarURL);
        yield return googleResp;
       googleRespStr = googleResp.text;
       print (googleRespStr);       
    }
}

I am getting correct latitude and longitude coordinates but for place, all I get is this,

{
   "debug_info" : [],
   "html_attributions" : [],
   "results" : [],
   "status" : "REQUEST_DENIED"
}

It would be great if someone could help me out with this...

Thanks in advance!

Edit: Download the JSON files from here and extract and put the folder in Assets folder of your Unity3d project. Refer the above code.


Solution

  • got it working. turns out, if you leave the SHA-1 fingerprint field empty, then also google create API key which can be used in the app.