private String extractCity(GeocoderResult input) { for (GeocoderAddressComponent component : input.getAddressComponents()) { if (component.getTypes().contains("locality")) { return component.getLongName(); } } return ""; }
@Override public void execute(Tuple input, BasicOutputCollector collector) { String address = input.getStringByField("address"); Long time = input.getLongByField("time"); GeocoderRequest request = new GeocoderRequestBuilder().setAddress(address).setLanguage("en").getGeocoderRequest(); GeocodeResponse response = geocoder.geocode(request); GeocoderStatus status = response.getStatus(); if (GeocoderStatus.OK.equals(status)) { GeocoderResult firstResult = response.getResults().get(0); LatLng latLng = firstResult.getGeometry().getLocation(); Long timeInterval = time / (15 * 1000); String city = extractCity(firstResult); collector.emit(new Values(timeInterval, latLng, city)); } }