我想做反向地理编码。我有lat long并希望API返回我的位置名称。
为此,我编码如下:
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();
}
上面的代码确定,但返回详细的回复
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}]}
我只需要短名称,即“白沙瓦大学”
,而不是长期响应我该怎么做?
答案 0 :(得分:1)
响应中包含“结果”列表。你可以通过
来了解他们List<GeocoderResult> results = geocoderResponse.getResults();
for( GeocoderResult geores: results ){
if( geores.getTypes().contains( "sublocality" ) ){
System.out.println( "Address: " + geores.getFormattedAddress() );
}
}
打印
Address: University Campus, Peshawar, Pakistan