如何使用附近的地方,服务器密钥或API密钥

时间:2016-11-17 05:04:59

标签: android google-maps google-maps-api-3 google-api google-nearby

  

我正在使用谷歌地图,附近的地方,距离矩阵。我试图使用此Google Recommended Process获取API密钥。我根据文档添加了调试密钥和释放密钥。但只有地图正在运作。附近的地方不是。通过谷歌浏览,有一些建议使用服务器密钥'。但是没有这样的服务器密钥'开发者控制台中的选项

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

    获取附近地点的代码:

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

0 个答案:

没有答案