I am working with google map, nearby places, distance matrix. I was trying to get an API key using this Google Recommended Process. I have added debug key and release key with it according to documentation. But only map is working. Nearby place is not. By surfing on google, got some suggestion to use 'server key'. But there is no such 'server key' option in develoeprs console
- What should I do now?
- If i need to use server key, then how can I get that?
Code to get Nearby Places:
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();
}
}