Created
August 14, 2013 12:23
-
-
Save masazdream/6230576 to your computer and use it in GitHub Desktop.
google apiを使ったジオコーダーと逆ジオコーダー
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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