我正在使用谷歌地图,附近的地方,距离矩阵。我试图使用此Google Recommended Process获取API密钥。我根据文档添加了调试密钥和释放密钥。但只有地图正在运作。附近的地方不是。通过谷歌浏览,有一些建议使用服务器密钥'。但是没有这样的服务器密钥'开发者控制台中的选项
- 我现在该怎么办?
- 如果我需要使用服务器密钥,那我该怎么办呢?
醇>获取附近地点的代码:
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();
}
}