谷歌为unity3d部署API

ykejflvf  于 2023-03-03  发布在  其他
关注(0)|答案(2)|浏览(146)

祝大家新年快乐!!
有人使用过Google Places API for Unity3D吗?我正在尝试使用Unity3D中的Places API制作一个增强现实应用程序。我遵循了这里提到的所有步骤
我首先获取了设备坐标,并将这些lat-lon输入到places api url中以获得JSON响应(为Android创建一个API密钥)。

#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);       
    }
}

我得到了正确的纬度和经度坐标但是对于地点,我得到的只有这个,

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

如果有人能帮我就太好了...
先谢了!
编辑:从here下载JSON文件,提取并将文件夹放入Unity3d项目的Assets文件夹中。参考以上代码。

g6baxovj

g6baxovj1#

得到了它的工作。原来,如果你离开SHA-1指纹字段为空,那么也谷歌创建API密钥,可以在应用程序中使用。

qvtsj1bj

qvtsj1bj2#

如何在Unity输入字段中使用您的代码?
我想尝试构建Unity项目,当用户输入地址时,它将显示或列出输入字段下方的预测地址,就像Uber输入地址一样。

相关问题