0

我正在使用谷歌地图、附近的地方、距离矩阵。我试图使用这个Google Recommended Process获取 API 密钥。我根据文档添加了调试密钥和发布密钥。但只有地图有效。附近的地方没有。通过在谷歌上冲浪,得到了一些使用“服务器密钥”的建议。但是 develoeprs 控制台中没有这样的“服务器密钥”选项

  1. 我现在该怎么办?
  2. 如果我需要使用服务器密钥,那么我该如何获得呢?

获取附近地点的代码:

public class PlacesService {

private String API_KEY;

public PlacesService(String apikey) {
    this.API_KEY = apikey;
}

public void setApiKey(String apikey) {
    this.API_KEY = apikey;
}

public ArrayList<Place> findPlaces(double latitude, double longitude,
                                   String placeSpacification) {

    String urlString = makeUrl(latitude, longitude, placeSpacification);

    try {
        String json = getJSON(urlString);


        JSONObject object = new JSONObject(json);
        JSONArray array = object.getJSONArray("results");

        ArrayList<Place> arrayList = new ArrayList<Place>();
        for (int i = 0; i < array.length(); i++) {
            try {
                Place place = Place
                        .jsonToPontoReferencia((JSONObject) array.get(i));
               // Log.v("Places Services ", "" + place);
                arrayList.add(place);
            } catch (Exception e) {
               // Log.e("Jhamel",e.toString());
            }
        }
        return arrayList;
    } catch (JSONException ex) {
        Logger.getLogger(PlacesService.class.getName()).log(Level.SEVERE,
                null, ex);

    }
    return null;
}

// https://maps.googleapis.com/maps/api/place/search/json?location=28.632808,77.218276&radius=500&types=atm&sensor=false&key=apikey
private String makeUrl(double latitude, double longitude, String place) {
    StringBuilder urlString = new StringBuilder(
            "https://maps.googleapis.com/maps/api/place/search/json?");

    if (place.equals("")) {
        urlString.append("&location=");
        urlString.append(Double.toString(latitude));
        urlString.append(",");
        urlString.append(Double.toString(longitude));
        urlString.append("&radius=5000");
        // urlString.append("&types="+place);
        urlString.append("&sensor=true&key=" + API_KEY);
    } else {
        urlString.append("&location=");
        urlString.append(Double.toString(latitude));
        urlString.append(",");
        urlString.append(Double.toString(longitude));
        urlString.append("&radius=5000");
        urlString.append("&types=" + place);
        urlString.append("&sensor=true&key=" + API_KEY);
    }

    Log.e("url", String.valueOf(urlString));

    return urlString.toString();
}

protected String getJSON(String url) {
    return getUrlContents(url);
}

private String getUrlContents(String theUrl) {
    StringBuilder content = new StringBuilder();

    try {
        URL url = new URL(theUrl);
        URLConnection urlConnection = url.openConnection();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(urlConnection.getInputStream()), 8);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            content.append(line + "\n");
        }
        bufferedReader.close();
    }catch (Exception e) {
        e.printStackTrace();
    }
    return content.toString();
}
}
4

0 回答 0