首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不使用google获取位置数据-此API项目未被授权使用此API。

不使用google获取位置数据-此API项目未被授权使用此API。
EN

Stack Overflow用户
提问于 2015-07-28 05:53:12
回答 1查看 1.9K关注 0票数 0

我使用的代码来自这里提出的问题,id。我更改了代码,将其添加到我的应用程序中。当我运行代码时,当我运行代码时,它会给我以下响应

代码语言:javascript
运行
复制
07-28 11:17:19.244: E/result(350): {"error_message":"This API project is not authorized to use this API. Please ensure that this API is activated in the APIs Console: Learn more: https:\/\/code.google.com\/apis\/console","predictions":[],"status":"REQUEST_DENIED"}

我在下面的线程中搜索了一些线程

  1. Maps API keys for geocoding not working
  2. This IP, site or mobile application is not authorized to use this API key和其他一两个

我按照建议做了以下步骤。

  1. 从控制台创建服务器密钥并将其添加到项目中。
  2. 从控制台启用Places API
  3. 从控制台启用Geocoding API

你可以在下面的图片中看到。

当我运行代码时,它仍然显示上面提到的错误。需要启用控制台中的任何其他API。

除了这个地图效果不错。如果您想知道这是我在清单中添加的api密钥。

代码语言:javascript
运行
复制
<!-- Goolge Maps API Key Development mode -->
            <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAbo3luzbyQTuuPLOyo_Ml***********" />

我将直接添加服务器密钥以请求url,如下所示

代码语言:javascript
运行
复制
private ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
    HttpURLConnection conn = null;
    StringBuilder jsonResults = new StringBuilder();
    try {
        StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
        sb.append("?key=" + API_KEY);
        sb.append("&input=" + URLEncoder.encode(input, "utf8"));

        URL url = new URL(sb.toString());
        conn = (HttpURLConnection) url.openConnection();
        InputStreamReader in = new InputStreamReader(conn.getInputStream());

        // Load the results into a StringBuilder
        int read;
        char[] buff = new char[1024];
        while ((read = in.read(buff)) != -1) {
            jsonResults.append(buff, 0, read);
        }
    } catch (MalformedURLException e) {
        Log.e(LOG_TAG, "Error processing Places API URL", e);
        return resultList;
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error connecting to Places API", e);
        return resultList;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }

    try {
        // Create a JSON object hierarchy from the results
        JSONObject jsonObj = new JSONObject(jsonResults.toString());
        JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
        Log.e("result",jsonObj.toString());
        // Extract the Place descriptions from the results
        resultList = new ArrayList<String>(predsJsonArray.length());
        for (int i = 0; i < predsJsonArray.length(); i++) {
            resultList.add(predsJsonArray.getJSONObject(i).getString("description")+ "," + predsJsonArray.getJSONObject(i).getString("place_id") );
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Cannot process JSON results", e);
    }
    return resultList;
}

请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-28 07:01:11

需要更深入的研究,经过一些尝试和错误,它起了作用。主要是我需要启用。而且很有魅力。

希望这对外面的人有帮助。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31668331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档