com.google.maps.GeolocationApi Java Examples

The following examples show how to use com.google.maps.GeolocationApi. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ArmeriaRequestHandler.java    From curiostack with MIT License 6 votes vote down vote up
private static Gson gsonForPolicy(FieldNamingPolicy fieldNamingPolicy) {
  return GSONS.computeIfAbsent(
      fieldNamingPolicy,
      policy ->
          new GsonBuilder()
              .registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeAdapter())
              .registerTypeAdapter(Distance.class, new DistanceAdapter())
              .registerTypeAdapter(Duration.class, new DurationAdapter())
              .registerTypeAdapter(Fare.class, new FareAdapter())
              .registerTypeAdapter(LatLng.class, new LatLngAdapter())
              .registerTypeAdapter(
                  AddressComponentType.class, new SafeEnumAdapter<>(AddressComponentType.UNKNOWN))
              .registerTypeAdapter(AddressType.class, new SafeEnumAdapter<>(AddressType.UNKNOWN))
              .registerTypeAdapter(TravelMode.class, new SafeEnumAdapter<>(TravelMode.UNKNOWN))
              .registerTypeAdapter(
                  LocationType.class, new SafeEnumAdapter<>(LocationType.UNKNOWN))
              .registerTypeAdapter(RatingType.class, new SafeEnumAdapter<>(RatingType.UNKNOWN))
              .registerTypeAdapter(DayOfWeek.class, new DayOfWeekAdapter())
              .registerTypeAdapter(PriceLevel.class, new PriceLevelAdapter())
              .registerTypeAdapter(Instant.class, new InstantAdapter())
              .registerTypeAdapter(LocalTime.class, new LocalTimeAdapter())
              .registerTypeAdapter(
                  GeolocationApi.Response.class, new GeolocationResponseAdapter())
              .setFieldNamingPolicy(policy)
              .create());
}
 
Example #2
Source File: NfcDeviceFragment.java    From ESeal with Apache License 2.0 4 votes vote down vote up
private void processGoogleMapGeocodeLocation() {
    GeoApiContext geoApiContext = new GeoApiContext().setApiKey(App.GOOGLE_MAP_API_KEY);

    GeolocationPayload.GeolocationPayloadBuilder payloadBuilder = new GeolocationPayload.GeolocationPayloadBuilder()
            .ConsiderIp(false);

    Activity activity = getActivity();
    CellTower.CellTowerBuilder cellTowerBuilder = new CellTower.CellTowerBuilder()
            .CellId(NetUtil.getCellLocationCid(activity))
            .LocationAreaCode(NetUtil.getCellLocationLac(activity))
            .MobileCountryCode(Integer.parseInt(NetUtil.getTelephonyNetWorkOperatorMcc(activity)))
            .MobileNetworkCode(Integer.parseInt(NetUtil.getTelephonyNetWorkOperatorMnc(activity)));

    payloadBuilder.AddCellTower(cellTowerBuilder.createCellTower());

    if (NetUtil.isWifi(getContext())) {
        WifiAccessPoint.WifiAccessPointBuilder wifiAccessPointBuilder = new WifiAccessPoint.WifiAccessPointBuilder()
                .MacAddress(NetUtil.getWifiMacAddress(activity))
                .SignalStrength(NetUtil.getWifiRssi(activity));

        payloadBuilder.AddWifiAccessPoint(wifiAccessPointBuilder.createWifiAccessPoint());

        wifiAccessPointBuilder = new WifiAccessPoint.WifiAccessPointBuilder()
                .MacAddress(NetUtil.getWifiInfo(activity).getBSSID());


        payloadBuilder.AddWifiAccessPoint(wifiAccessPointBuilder.createWifiAccessPoint());
    }


    GeolocationApi.geolocate(geoApiContext, payloadBuilder.createGeolocationPayload())
            .setCallback(new PendingResult.Callback<GeolocationResult>() {
                @Override
                public void onResult(GeolocationResult result) {
                    Log.d(TAG, "onResult() returned: " + result.location.toString());
                }

                @Override
                public void onFailure(Throwable e) {
                    Log.d(TAG, "onFailure() returned: " + e.getMessage());
                }
            });
}
 
Example #3
Source File: GeolocationResponseAdapter.java    From google-maps-services-java with Apache License 2.0 4 votes vote down vote up
/**
 * Reads in a JSON object to create a Geolocation Response. See:
 * https://developers.google.com/maps/documentation/geolocation/intro#responses
 *
 * <p>Success Case:
 *
 * <pre>
 *   {
 *     "location": {
 *       "lat": 51.0,
 *       "lng": -0.1
 *     },
 *     "accuracy": 1200.4
 *   }
 * </pre>
 *
 * Error Case: The response contains an object with a single error object with the following keys:
 *
 * <p>code: This is the same as the HTTP status of the response. {@code message}: A short
 * description of the error. {@code errors}: A list of errors which occurred. Each error contains
 * an identifier for the type of error (the reason) and a short description (the message). For
 * example, sending invalid JSON will return the following error:
 *
 * <pre>
 *   {
 *     "error": {
 *       "errors": [ {
 *           "domain": "geolocation",
 *           "reason": "notFound",
 *           "message": "Not Found",
 *           "debugInfo": "status: ZERO_RESULTS\ncom.google.api.server.core.Fault: Immu...
 *       }],
 *       "code": 404,
 *       "message": "Not Found"
 *     }
 *   }
 * </pre>
 */
@Override
public GeolocationApi.Response read(JsonReader reader) throws IOException {

  if (reader.peek() == JsonToken.NULL) {
    reader.nextNull();
    return null;
  }
  GeolocationApi.Response response = new GeolocationApi.Response();
  LatLngAdapter latLngAdapter = new LatLngAdapter();

  reader.beginObject(); // opening {
  while (reader.hasNext()) {
    String name = reader.nextName();
    // two different objects could be returned a success object containing "location" and
    // "accuracy" keys or an error object containing an "error" key
    if (name.equals("location")) {
      // we already have a parser for the LatLng object so lets use that
      response.location = latLngAdapter.read(reader);
    } else if (name.equals("accuracy")) {
      response.accuracy = reader.nextDouble();
    } else if (name.equals("error")) {
      reader.beginObject(); // the error key leads to another object...
      while (reader.hasNext()) {
        String errName = reader.nextName();
        // ...with keys "errors", "code" and "message"
        if (errName.equals("code")) {
          response.code = reader.nextInt();
        } else if (errName.equals("message")) {
          response.message = reader.nextString();
        } else if (errName.equals("errors")) {
          reader.beginArray(); // its plural because its an array of errors...
          while (reader.hasNext()) {
            reader.beginObject(); // ...and each error array element is an object...
            while (reader.hasNext()) {
              errName = reader.nextName();
              // ...with keys "reason", "domain", "debugInfo", "location", "locationType",  and
              // "message" (again)
              if (errName.equals("reason")) {
                response.reason = reader.nextString();
              } else if (errName.equals("domain")) {
                response.domain = reader.nextString();
              } else if (errName.equals("debugInfo")) {
                response.debugInfo = reader.nextString();
              } else if (errName.equals("message")) {
                // have this already
                reader.nextString();
              } else if (errName.equals("location")) {
                reader.nextString();
              } else if (errName.equals("locationType")) {
                reader.nextString();
              }
            }
            reader.endObject();
          }
          reader.endArray();
        }
      }
      reader.endObject(); // closing }
    }
  }
  reader.endObject();
  return response;
}
 
Example #4
Source File: GeolocationResponseAdapter.java    From google-maps-services-java with Apache License 2.0 4 votes vote down vote up
/** Not supported. */
@Override
public void write(JsonWriter out, GeolocationApi.Response value) throws IOException {
  throw new UnsupportedOperationException("Unimplemented method.");
}