谷歌为unity3d放置api

时间:2014-01-02 05:51:01

标签: api unity3d google-places-api

祝你新年快乐!

是否有人使用过适用于Unity3D的Google Places API?我正在尝试使用Unity3D中的Places API制作增强现实应用。我按照here

提到的所有步骤进行了操作

我首先获取了设备坐标并将这些lat-lon提供给了地址api url以获取JSON响应(为Android创建API密钥)。以下是我的Unity3D C#代码,

#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文件夹中。请参阅上面的代码。

1 个答案:

答案 0 :(得分:0)

让它运转起来。事实证明,如果你把SHA-1指纹字段留空,那么谷歌创建可以在应用程序中使用的API密钥。