我正在开发用于跟踪作为Web应用程序的项目。现在我需要从给定的经度和经度访问街道地址,州和国家。 所以我需要代码。如何从纬度和经度访问地址位置?
我必须使用的Google API: https://maps.googleapis.com/maps/api/geocode/outputFormat?parameters
我还需要使用JSON反向地理编码吗?
答案 0 :(得分:0)
我这样做,(代码是自我解释):
[...]
GeoApiContext context = new GeoApiContext().setApiKey("your-key");
GeocodingApiRequest request = GeocodingApi.newRequest(context);
request.latlng(new LatLng(lat, lon));
request.resultType(AddressType.STREET_ADDRESS);
GeocodingResult[] results = request.await(); // try catch?
for(GeocodingResult r : results){
for (AddressComponent ac : r.addressComponents) {
System.out.println(ac);
}
}
同时检查AddressComponentType:
for(GeocodingResult r : results){
for (AddressComponent ac : r.addressComponents) {
for (AddressComponentType acType : ac.types) {
if (acType == AddressComponentType.LOCALITY) {
System.out.println(ac.longName);
} else if (acType == AddressComponentType.STREET_ADDRESS) {
System.out.println(ac.longName);
} else if (acType == AddressComponentType.COUNTRY) {
System.out.println(ac.longName);
}
}
}
}
别忘了包含maven依赖:
<dependency>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.1.15</version>
</dependency>