0

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

  1. What should I do now?
  2. 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();
}
}
  • You need to give permission in manifest as well as take permission granted for marshmallow and lollipop device – Binesh Kumar Nov 17 '16 at 06:01
  • I have added all the necessary permissions in all respected places. Even checking the permission each time for marshmallow. – Razon Hossain Nov 17 '16 at 06:29
  • Please share you google map code – Binesh Kumar Nov 17 '16 at 06:31
  • Do you use any restriction on API key? Google Maps Web Services allow only IP restrictions. Otherwise the API key without restrictions should work. Currently Google does not have a Server or Browser API keys, it was some time ago. Now you can create an API key and apply certain restriction. Do you receive any error message from Places API? – xomena Nov 17 '16 at 20:48
  • Yes, I have added Android Restrictions because i am using it in android. What type of error are you indicating? – Razon Hossain Nov 18 '16 at 06:10

0 Answers0