@Test public void reverseGeoCode() { double latitude = 16.56163928157282; double longitude = 81.99713218957186; String cityName = "Amalapuram"; Optional<Location> location = locationService.reverseGeoCode(latitude, longitude); location.ifPresent(location1 -> assertTrue("Correct city is being returned", location1.getCity().equalsIgnoreCase(cityName))); }
public Optional<Location> reverseGeoCode(double lat, double lng) { try { GeocodingResult[] gResp = GeocodingApi.newRequest(gtx).latlng(new LatLng(lat, lng)).region("IN") .await(); Location location = new Location(); for (AddressComponent addressComponent : gResp[0].addressComponents) { List<AddressComponentType> addressComponentTypes = Arrays.asList(addressComponent.types); if (addressComponentTypes.contains(AddressComponentType.COUNTRY)) location.setCountry(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.ADMINISTRATIVE_AREA_LEVEL_1)) location.setState(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.ADMINISTRATIVE_AREA_LEVEL_2)) location.setDistrict(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.LOCALITY)) location.setCity(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.POSTAL_CODE)) location.setPostalCode(addressComponent.longName); } return Optional.of(location); } catch (InvalidRequestException e) { LOG.error("Invalid latitude longitude pair provided. "); return Optional.empty(); } catch (Exception e) { LOG.error("Unable to reverse geocode"); return Optional.empty(); } }
GoogleLocationService { public Optional<Location> reverseGeoCode(double lat, double lng) { try { GeocodingResult[] gResp = GeocodingApi.newRequest(gtx).latlng(new LatLng(lat, lng)).region("IN") .await(); Location location = new Location(); for (AddressComponent addressComponent : gResp[0].addressComponents) { List<AddressComponentType> addressComponentTypes = Arrays.asList(addressComponent.types); if (addressComponentTypes.contains(AddressComponentType.COUNTRY)) location.setCountry(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.ADMINISTRATIVE_AREA_LEVEL_1)) location.setState(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.ADMINISTRATIVE_AREA_LEVEL_2)) location.setDistrict(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.LOCALITY)) location.setCity(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.POSTAL_CODE)) location.setPostalCode(addressComponent.longName); } return Optional.of(location); } catch (InvalidRequestException e) { LOG.error("Invalid latitude longitude pair provided. "); return Optional.empty(); } catch (Exception e) { LOG.error("Unable to reverse geocode"); return Optional.empty(); } } }
GoogleLocationService { public Optional<Location> reverseGeoCode(double lat, double lng) { try { GeocodingResult[] gResp = GeocodingApi.newRequest(gtx).latlng(new LatLng(lat, lng)).region("IN") .await(); Location location = new Location(); for (AddressComponent addressComponent : gResp[0].addressComponents) { List<AddressComponentType> addressComponentTypes = Arrays.asList(addressComponent.types); if (addressComponentTypes.contains(AddressComponentType.COUNTRY)) location.setCountry(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.ADMINISTRATIVE_AREA_LEVEL_1)) location.setState(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.ADMINISTRATIVE_AREA_LEVEL_2)) location.setDistrict(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.LOCALITY)) location.setCity(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.POSTAL_CODE)) location.setPostalCode(addressComponent.longName); } return Optional.of(location); } catch (InvalidRequestException e) { LOG.error("Invalid latitude longitude pair provided. "); return Optional.empty(); } catch (Exception e) { LOG.error("Unable to reverse geocode"); return Optional.empty(); } }  }
GoogleLocationService { public Optional<Location> reverseGeoCode(double lat, double lng) { try { GeocodingResult[] gResp = GeocodingApi.newRequest(gtx).latlng(new LatLng(lat, lng)).region("IN") .await(); Location location = new Location(); for (AddressComponent addressComponent : gResp[0].addressComponents) { List<AddressComponentType> addressComponentTypes = Arrays.asList(addressComponent.types); if (addressComponentTypes.contains(AddressComponentType.COUNTRY)) location.setCountry(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.ADMINISTRATIVE_AREA_LEVEL_1)) location.setState(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.ADMINISTRATIVE_AREA_LEVEL_2)) location.setDistrict(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.LOCALITY)) location.setCity(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.POSTAL_CODE)) location.setPostalCode(addressComponent.longName); } return Optional.of(location); } catch (InvalidRequestException e) { LOG.error("Invalid latitude longitude pair provided. "); return Optional.empty(); } catch (Exception e) { LOG.error("Unable to reverse geocode"); return Optional.empty(); } }  Optional<Location> reverseGeoCode(double lat, double lng); }
GoogleLocationService { public Optional<Location> reverseGeoCode(double lat, double lng) { try { GeocodingResult[] gResp = GeocodingApi.newRequest(gtx).latlng(new LatLng(lat, lng)).region("IN") .await(); Location location = new Location(); for (AddressComponent addressComponent : gResp[0].addressComponents) { List<AddressComponentType> addressComponentTypes = Arrays.asList(addressComponent.types); if (addressComponentTypes.contains(AddressComponentType.COUNTRY)) location.setCountry(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.ADMINISTRATIVE_AREA_LEVEL_1)) location.setState(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.ADMINISTRATIVE_AREA_LEVEL_2)) location.setDistrict(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.LOCALITY)) location.setCity(addressComponent.longName); else if (addressComponentTypes.contains(AddressComponentType.POSTAL_CODE)) location.setPostalCode(addressComponent.longName); } return Optional.of(location); } catch (InvalidRequestException e) { LOG.error("Invalid latitude longitude pair provided. "); return Optional.empty(); } catch (Exception e) { LOG.error("Unable to reverse geocode"); return Optional.empty(); } }  Optional<Location> reverseGeoCode(double lat, double lng);  }