Skip to content

Instantly share code, notes, and snippets.

@masazdream
Created August 14, 2013 12:23
Show Gist options
  • Select an option

  • Save masazdream/6230576 to your computer and use it in GitHub Desktop.

Select an option

Save masazdream/6230576 to your computer and use it in GitHub Desktop.
google apiを使ったジオコーダーと逆ジオコーダー
public class GeocoderApp {
public static String invGeocode(double lat, double lng){
final Geocoder geocoder = new Geocoder();
BigDecimal latB = new BigDecimal(lat);
BigDecimal lngB = new BigDecimal(lng);
LatLng location = new LatLng(latB, lngB);
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setLocation(location).setLanguage("ja").getGeocoderRequest();
GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
GeocoderStatus status = geocoderResponse.getStatus();
switch (status) {
case ZERO_RESULTS:
return null;
case OVER_QUERY_LIMIT:
case REQUEST_DENIED:
case INVALID_REQUEST:
case UNKNOWN_ERROR:
case ERROR:
throw new RuntimeException(status.value());
default:
}
List<GeocoderResult> results = geocoderResponse.getResults();
for (GeocoderResult result : results) {
return result.getFormattedAddress();
}
return null;
}
public static LatLng geocode(String address) {
final Geocoder geocoder = new Geocoder();
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder()
.setAddress(address).setLanguage("ja")
.getGeocoderRequest();
GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
GeocoderStatus status = geocoderResponse.getStatus();
switch (status) {
case ZERO_RESULTS:
return null;
case OVER_QUERY_LIMIT:
case REQUEST_DENIED:
case INVALID_REQUEST:
case UNKNOWN_ERROR:
case ERROR:
throw new RuntimeException(status.value());
default:
}
List<GeocoderResult> results = geocoderResponse.getResults();
for (GeocoderResult result : results) {
GeocoderGeometry geometry = result.getGeometry();
return geometry.getLocation();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment