コード例 #1
0
  private void getPlaces() {
    // Get some cool places!

    Location location = LocationHelper.getLocation(this);

    if (location != null) {
      Log.i(TAG, "Location!");
    }
    double latitude = 33.8303090;
    double longitude = -118.3068440;

    try {
      Request request =
          new Request.Builder()
              .url(
                  "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key="
                      + getString(R.string.apiKey)
                      + "&"
                      + "location="
                      + latitude
                      + ","
                      + longitude
                      + "&"
                      + "radius="
                      + 500
                      + "&"
                      + "type=food"
                      + "&"
                      + "rankBy=distance") //  500 meters
              .build();

      Log.i(TAG, request.toString());

      okHttpClient
          .newCall(request)
          .enqueue(
              new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {
                  // SOMETHING WENT WRONG
                  // TODO: Insert AlertDialog here!

                }

                @Override
                public void onResponse(Response response) throws IOException {
                  // SOMETHING AWESOME HAPPENED
                  Handler uiThread = new Handler(getBaseContext().getMainLooper());
                  JsonParser parser = new JsonParser();
                  String data = response.body().string();
                  JsonElement element = parser.parse(data);

                  if (element.isJsonObject()) {
                    JsonArray results = element.getAsJsonObject().get("results").getAsJsonArray();

                    for (int i = 0; i < results.size(); i++) {
                      JsonObject place = results.get(i).getAsJsonObject();
                      String name = place.get("name").getAsString();
                      String address = place.get("vicinity").getAsString();
                      boolean isOpen;

                      if (place.has("opening_hours")) {
                        JsonObject openHours = place.get("opening_hours").getAsJsonObject();
                        isOpen = openHours.get("open_now").getAsBoolean();
                      } else {
                        isOpen = false;
                      }

                      Place item = new Place(name, address, isOpen);

                      model.add(item);
                    }

                    uiThread.post(
                        new Runnable() {
                          @Override
                          public void run() {
                            buildListview();
                          }
                        });
                  } else {
                    Log.e(TAG, "=====FAILURE=====");
                    Log.e(TAG, "element is not a JsonElement");
                  }
                }
              });

    } catch (Exception e) {
      e.printStackTrace();
    }
  }