I want to do reverse geocoding. I have lat long and want API to return me the name of location.
For this I coded as below:
Double l1 = 34.010920600000000000;
Double l2 = 71.480397199999970000;
BigDecimal lat = new BigDecimal(l1, MathContext.DECIMAL64);;
BigDecimal lng = new BigDecimal(l2, MathContext.DECIMAL64);;
LatLng location = new LatLng(lat, lng);
//GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress("Paris, France").setLanguage("en").getGeocoderRequest();
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setLocation(location).setLanguage("en").getGeocoderRequest();
try {
GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
System.out.println(geocoderResponse);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Above code OK and but returns a detailed response as
GeocodeResponse{status=OK, results=[GeocoderResult{types=[route], formattedAddress='Unnamed Road, University of Peshawar, Peshawar, Pakistan', addressComponents=[GeocoderAddressComponent{longName='Unnamed Road', shortName='Unnamed Road', types=[route]}, GeocoderAddressComponent{longName='University of Peshawar', shortName='University of Peshawar', types=[establishment]}, GeocoderAddressComponent{longName='University Campus', shortName='University Campus', types=[sublocality_level_1, sublocality, political]}, GeocoderAddressComponent{longName='Peshawar', shortName='Peshawar', types=[locality, political]}, GeocoderAddressComponent{longName='Peshawar District', shortName='Peshawar District', types=[administrative_area_level_2, political]}, GeocoderAddressComponent{longName='Khyber Pakhtunkhwa', shortName='N.W.F.P', types=[administrative_area_level_1, political]}, GeocoderAddressComponent{longName='Pakistan', shortName='PK', types=[country, political]}], geometry=GeocoderGeometry{location=LatLng{lat=34.0119081, lng=71.4811938}, locationType=APPROXIMATE, viewport=LatLngBounds{southwest=LatLng{lat=34.01014111970849, lng=71.4797176}, northeast=LatLng{lat=34.01283908029149, lng=71.4832237}}, bounds=LatLngBounds{southwest=LatLng{lat=34.0106784, lng=71.4797176}, northeast=LatLng{lat=34.0123018, lng=71.4832237}}}, partialMatch=false}, GeocoderResult{types=[sublocality_level_1, sublocality, political], formattedAddress='University Campus, Peshawar, Pakistan', addressComponents=[GeocoderAddressComponent{longName='University Campus', shortName='University Campus', types=[sublocality_level_1, sublocality, political]}, GeocoderAddressComponent{longName='Peshawar', shortName='Peshawar', types=[locality, political]}, GeocoderAddressComponent{longName='Peshawar District', shortName='Peshawar District', types=[administrative_area_level_2, political]}, GeocoderAddressComponent{longName='Khyber Pakhtunkhwa', shortName='N.W.F.P', types=[administrative_area_level_1, political]}, GeocoderAddressComponent{longName='Pakistan', shortName='PK', types=[country, political]}], geometry=GeocoderGeometry{location=LatLng{lat=34.0024586, lng=71.48068769999999}, locationType=APPROXIMATE, viewport=LatLngBounds{southwest=LatLng{lat=33.9979856, lng=71.4711176}, northeast=LatLng{lat=34.0235887, lng=71.4894956}}, bounds=LatLngBounds{southwest=LatLng{lat=33.9979856, lng=71.4711176}, northeast=LatLng{lat=34.0235887, lng=71.4894956}}}, partialMatch=false}, GeocoderResult{types=[locality, political], formattedAddress='Peshawar, Pakistan', addressComponents=[GeocoderAddressComponent{longName='Peshawar', shortName='Peshawar', types=[locality, political]}, GeocoderAddressComponent{longName='Peshawar District', shortName='Peshawar District', types=[administrative_area_level_2, political]}, GeocoderAddressComponent{longName='Khyber Pakhtunkhwa', shortName='N.W.F.P', types=[administrative_area_level_1, political]}, GeocoderAddressComponent{longName='Pakistan', shortName='PK', types=[country, political]}], geometry=GeocoderGeometry{location=LatLng{lat=34.0149748, lng=71.5804899}, locationType=APPROXIMATE, viewport=LatLngBounds{southwest=LatLng{lat=33.9420782, lng=71.4032364}, northeast=LatLng{lat=34.0412096, lng=71.62399289999999}}, bounds=LatLngBounds{southwest=LatLng{lat=33.9420782, lng=71.4032364}, northeast=LatLng{lat=34.0412096, lng=71.62399289999999}}}, partialMatch=false}, GeocoderResult{types=[administrative_area_level_2, political], formattedAddress='Peshawar District, Pakistan', addressComponents=[GeocoderAddressComponent{longName='Peshawar District', shortName='Peshawar District', types=[administrative_area_level_2, political]}, GeocoderAddressComponent{longName='Khyber Pakhtunkhwa', shortName='N.W.F.P', types=[administrative_area_level_1, political]}, GeocoderAddressComponent{longName='Pakistan', shortName='PK', types=[country, political]}], geometry=GeocoderGeometry{location=LatLng{lat=33.9436765, lng=71.6198686}, locationType=APPROXIMATE, viewport=LatLngBounds{southwest=LatLng{lat=33.721359, lng=71.3644008}, northeast=LatLng{lat=34.195384, lng=71.8258666}}, bounds=LatLngBounds{southwest=LatLng{lat=33.721359, lng=71.3644008}, northeast=LatLng{lat=34.195384, lng=71.8258666}}}, partialMatch=false}, GeocoderResult{types=[administrative_area_level_1, political], formattedAddress='Khyber Pakhtunkhwa, Pakistan', addressComponents=[GeocoderAddressComponent{longName='Khyber Pakhtunkhwa', shortName='N.W.F.P', types=[administrative_area_level_1, political]}, GeocoderAddressComponent{longName='Pakistan', shortName='PK', types=[country, political]}], geometry=GeocoderGeometry{location=LatLng{lat=34.9526205, lng=72.331113}, locationType=APPROXIMATE, viewport=LatLngBounds{southwest=LatLng{lat=31.1910449, lng=70.0552089}, northeast=LatLng{lat=36.91116, lng=74.13702099999999}}, bounds=LatLngBounds{southwest=LatLng{lat=31.1910449, lng=70.0552089}, northeast=LatLng{lat=36.91116, lng=74.13702099999999}}}, partialMatch=false}, GeocoderResult{types=[country, political], formattedAddress='Pakistan', addressComponents=[GeocoderAddressComponent{longName='Pakistan', shortName='PK', types=[country, political]}], geometry=GeocoderGeometry{location=LatLng{lat=30.375321, lng=69.34511599999999}, locationType=APPROXIMATE, viewport=LatLngBounds{southwest=LatLng{lat=23.6946945, lng=60.872972}, northeast=LatLng{lat=37.084107, lng=77.8334694}}, bounds=LatLngBounds{southwest=LatLng{lat=23.6946945, lng=60.872972}, northeast=LatLng{lat=37.084107, lng=77.8334694}}}, partialMatch=false}]}
Instead of above long response, I only need short location name i.e. "University of Peshawar"
How would I do that?
There's a list of "results" contained in the response. You can get at them by
List<GeocoderResult> results = geocoderResponse.getResults();
for( GeocoderResult geores: results ){
if( geores.getTypes().contains( "sublocality" ) ){
System.out.println( "Address: " + geores.getFormattedAddress() );
}
}
This prints
Address: University Campus, Peshawar, Pakistan