Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a more complete example (includes JSON parsing and some exception handling) for Places API search, autocomplete, and details. It was written for Android, but can be easily ported for non-Android use (need to include org.json libs and use different logging). The <code>Place</code> class is a simple value object.</p> <pre><code>package com.example.google.places; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; /** * @author saxman */ public class PlacesService { private static final String LOG_TAG = "ExampleApp"; private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place"; private static final String TYPE_AUTOCOMPLETE = "/autocomplete"; private static final String TYPE_DETAILS = "/details"; private static final String TYPE_SEARCH = "/search"; private static final String OUT_JSON = "/json"; // KEY! private static final String API_KEY = "YOUR KEY"; public static ArrayList&lt;Place&gt; autocomplete(String input) { ArrayList&lt;Place&gt; resultList = null; HttpURLConnection conn = null; StringBuilder jsonResults = new StringBuilder(); try { StringBuilder sb = new StringBuilder(PLACES_API_BASE); sb.append(TYPE_AUTOCOMPLETE); sb.append(OUT_JSON); sb.append("?sensor=false"); sb.append("&amp;key=" + API_KEY); sb.append("&amp;input=" + URLEncoder.encode(input, "utf8")); URL url = new URL(sb.toString()); conn = (HttpURLConnection) url.openConnection(); InputStreamReader in = new InputStreamReader(conn.getInputStream()); int read; char[] buff = new char[1024]; while ((read = in.read(buff)) != -1) { jsonResults.append(buff, 0, read); } } catch (MalformedURLException e) { Log.e(LOG_TAG, "Error processing Places API URL", e); return resultList; } catch (IOException e) { Log.e(LOG_TAG, "Error connecting to Places API", e); return resultList; } finally { if (conn != null) { conn.disconnect(); } } try { // Create a JSON object hierarchy from the results JSONObject jsonObj = new JSONObject(jsonResults.toString()); JSONArray predsJsonArray = jsonObj.getJSONArray("predictions"); // Extract the Place descriptions from the results resultList = new ArrayList&lt;Place&gt;(predsJsonArray.length()); for (int i = 0; i &lt; predsJsonArray.length(); i++) { Place place = new Place(); place.reference = predsJsonArray.getJSONObject(i).getString("reference"); place.name = predsJsonArray.getJSONObject(i).getString("description"); resultList.add(place); } } catch (JSONException e) { Log.e(LOG_TAG, "Error processing JSON results", e); } return resultList; } public static ArrayList&lt;Place&gt; search(String keyword, double lat, double lng, int radius) { ArrayList&lt;Place&gt; resultList = null; HttpURLConnection conn = null; StringBuilder jsonResults = new StringBuilder(); try { StringBuilder sb = new StringBuilder(PLACES_API_BASE); sb.append(TYPE_SEARCH); sb.append(OUT_JSON); sb.append("?sensor=false"); sb.append("&amp;key=" + API_KEY); sb.append("&amp;keyword=" + URLEncoder.encode(keyword, "utf8")); sb.append("&amp;location=" + String.valueOf(lat) + "," + String.valueOf(lng)); sb.append("&amp;radius=" + String.valueOf(radius)); URL url = new URL(sb.toString()); conn = (HttpURLConnection) url.openConnection(); InputStreamReader in = new InputStreamReader(conn.getInputStream()); int read; char[] buff = new char[1024]; while ((read = in.read(buff)) != -1) { jsonResults.append(buff, 0, read); } } catch (MalformedURLException e) { Log.e(LOG_TAG, "Error processing Places API URL", e); return resultList; } catch (IOException e) { Log.e(LOG_TAG, "Error connecting to Places API", e); return resultList; } finally { if (conn != null) { conn.disconnect(); } } try { // Create a JSON object hierarchy from the results JSONObject jsonObj = new JSONObject(jsonResults.toString()); JSONArray predsJsonArray = jsonObj.getJSONArray("results"); // Extract the Place descriptions from the results resultList = new ArrayList&lt;Place&gt;(predsJsonArray.length()); for (int i = 0; i &lt; predsJsonArray.length(); i++) { Place place = new Place(); place.reference = predsJsonArray.getJSONObject(i).getString("reference"); place.name = predsJsonArray.getJSONObject(i).getString("name"); resultList.add(place); } } catch (JSONException e) { Log.e(LOG_TAG, "Error processing JSON results", e); } return resultList; } public static Place details(String reference) { HttpURLConnection conn = null; StringBuilder jsonResults = new StringBuilder(); try { StringBuilder sb = new StringBuilder(PLACES_API_BASE); sb.append(TYPE_DETAILS); sb.append(OUT_JSON); sb.append("?sensor=false"); sb.append("&amp;key=" + API_KEY); sb.append("&amp;reference=" + URLEncoder.encode(reference, "utf8")); URL url = new URL(sb.toString()); conn = (HttpURLConnection) url.openConnection(); InputStreamReader in = new InputStreamReader(conn.getInputStream()); // Load the results into a StringBuilder int read; char[] buff = new char[1024]; while ((read = in.read(buff)) != -1) { jsonResults.append(buff, 0, read); } } catch (MalformedURLException e) { Log.e(LOG_TAG, "Error processing Places API URL", e); return null; } catch (IOException e) { Log.e(LOG_TAG, "Error connecting to Places API", e); return null; } finally { if (conn != null) { conn.disconnect(); } } Place place = null; try { // Create a JSON object hierarchy from the results JSONObject jsonObj = new JSONObject(jsonResults.toString()).getJSONObject("result"); place = new Place(); place.icon = jsonObj.getString("icon"); place.name = jsonObj.getString("name"); place.formatted_address = jsonObj.getString("formatted_address"); if (jsonObj.has("formatted_phone_number")) { place.formatted_phone_number = jsonObj.getString("formatted_phone_number"); } } catch (JSONException e) { Log.e(LOG_TAG, "Error processing JSON results", e); } return place; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload