1

これは、Geo-coder から応答を取得するための私のコードです。

public class Snippet {
    private GeoCodeResponse getAddressFromLatLng(double lattitute,
            double longtitute) throws ClientProtocolException, IOException {
        String latitude = String.valueOf(lattitute);
        String longitude = String.valueOf(longtitute);
        String URL = "http://maps.google.com/maps/api/geocode/json?latlng="
                + latitude + "," + longitude + "&sensor=true";

        RestTemplate restTemplate = new RestTemplate();
        ObjectMapper mapper = new ObjectMapper();
        String forObject = restTemplate.getForObject(URL, String.class);
        GeoCodeResponse geoCodeResponse = mapper.readValue(forObject,
                GeoCodeResponse.class);

        System.out.println(forObject);
        return geoCodeResponse;
    }
    public static void main(String[] args) throws ClientProtocolException,
                IOException {
            GeoCodeResponse geoCoderResponse = new TrackServiceImpl()
                    .getAddressFromLatLng(17.42560, 78.548339);
            if (geoCoderResponse.getStatus().equals("OK")) {

                List<Results> listOfResults = geoCoderResponse.getResults();

                for (Results result : listOfResults) {
                    // get Results Object
                    Iterator<Results> results = listOfResults.iterator();

                    while (results.hasNext()) {
                        result = results.next();
                        System.out.println(result.getFormatted_address());
                        // get Address Object
                        Collection<AddressComponent> addresscomponents = result
                                .getAddress_components();
                        Iterator<AddressComponent> addresscomponentsIterator = addresscomponents
                                .iterator();
                        while (addresscomponentsIterator.hasNext()) {
                            AddressComponent addressComponent = addresscomponentsIterator
                                    .next();
                            System.out.println(addressComponent.getLong_name());
                            System.out.println(addressComponent.getShort_name());
                        }
                        // get Geometry Object
                        Geometry geometry = result.getGeometry();
                        System.out.println(geometry.getLocation_type());

                        // get Location Object
                        Location location = geometry.getLocation();
                        System.out.println(location.getLat());
                        System.out.println(location.getLng());

                    }
                    break;
                }
            }
}

    private GeoCodeResponse getAddressFromLatLng(double lattitute,
            double longtitute) throws ClientProtocolException, IOException {
        String latitude = String.valueOf(lattitute);
        String longitude = String.valueOf(longtitute);
        String URL = "http://maps.google.com/maps/api/geocode/json?latlng="
                + latitude + "," + longitude + "&sensor=true";

        RestTemplate restTemplate = new RestTemplate();
        ObjectMapper mapper = new ObjectMapper();
        String forObject = restTemplate.getForObject(URL, String.class);
        GeoCodeResponse geoCodeResponse = mapper.readValue(forObject,
                GeoCodeResponse.class);

        System.out.println(forObject);
        return geoCodeResponse;
    }
}

これは、ジオ コーダーの応答です。

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "4-1-91/3",
               "short_name" : "4-1-91/3",
               "types" : []
            },
            {
               "long_name" : "Nacharam Mallapur Road",
               "short_name" : "Nacharam Mallapur Road",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Bhavani Nagar",
               "short_name" : "Bhavani Nagar",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Tarnaka",
               "short_name" : "Tarnaka",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Secunderabad",
               "short_name" : "SC",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Ranga Reddy",
               "short_name" : "R.R. District",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Andhra Pradesh",
               "short_name" : "AP",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "500076",
               "short_name" : "500076",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "4-1-91/3, Nacharam Mallapur Road, Bhavani Nagar, Tarnaka, Secunderabad, Andhra Pradesh 500076, India",
         "geometry" : {
            "location" : {
               "lat" : 17.4251743,
               "lng" : 78.5480111
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 17.4265232802915,
                  "lng" : 78.54936008029151
               },
               "southwest" : {
                  "lat" : 17.4238253197085,
                  "lng" : 78.54666211970849
               }
            }
         },
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Bhavani Nagar",
               "short_name" : "Bhavani Nagar",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Tarnaka",
               "short_name" : "Tarnaka",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Secunderabad",
               "short_name" : "SC",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Ranga Reddy",
               "short_name" : "R.R. District",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Andhra Pradesh",
               "short_name" : "AP",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Bhavani Nagar, Tarnaka, Secunderabad, Andhra Pradesh, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 17.4276068,
                  "lng" : 78.550246
               },
               "southwest" : {
                  "lat" : 17.4249009,
                  "lng" : 78.54588509999999
               }
            },
            "location" : {
               "lat" : 17.4261285,
               "lng" : 78.5479845
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 17.4276068,
                  "lng" : 78.550246
               },
               "southwest" : {
                  "lat" : 17.4249009,
                  "lng" : 78.54588509999999
               }
            }
         },
         "types" : [ "sublocality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "500076",
               "short_name" : "500076",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Andhra Pradesh",
               "short_name" : "AP",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Andhra Pradesh 500076, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 17.4503108,
                  "lng" : 78.59751469999999
               },
               "southwest" : {
                  "lat" : 17.4199893,
                  "lng" : 78.5419289
               }
            },
            "location" : {
               "lat" : 17.4381342,
               "lng" : 78.56040689999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 17.4503108,
                  "lng" : 78.59751469999999
               },
               "southwest" : {
                  "lat" : 17.4199893,
                  "lng" : 78.5419289
               }
            }
         },
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Hyderabad",
               "short_name" : "Hyderabad",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Andhra Pradesh",
               "short_name" : "AP",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Hyderabad, Andhra Pradesh, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 17.4823267,
                  "lng" : 78.5604858
               },
               "southwest" : {
                  "lat" : 17.3486722,
                  "lng" : 78.4018707
               }
            },
            "location" : {
               "lat" : 17.385044,
               "lng" : 78.486671
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 17.4823267,
                  "lng" : 78.5604858
               },
               "southwest" : {
                  "lat" : 17.3486722,
                  "lng" : 78.4018707
               }
            }
         },
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Secunderabad",
               "short_name" : "SC",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Hyderabad",
               "short_name" : "Hyderabad",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Andhra Pradesh",
               "short_name" : "AP",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Secunderabad, Andhra Pradesh, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 17.6393052,
                  "lng" : 78.64442819999999
               },
               "southwest" : {
                  "lat" : 17.4124778,
                  "lng" : 78.47253499999999
               }
            },
            "location" : {
               "lat" : 17.4399295,
               "lng" : 78.4982741
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 17.6393052,
                  "lng" : 78.64442819999999
               },
               "southwest" : {
                  "lat" : 17.4124778,
                  "lng" : 78.47253499999999
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Ranga Reddy",
               "short_name" : "R.R. District",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Andhra Pradesh",
               "short_name" : "AP",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Ranga Reddy, Andhra Pradesh, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 17.701268,
                  "lng" : 78.8475038
               },
               "southwest" : {
                  "lat" : 16.8492337,
                  "lng" : 77.3753358
               }
            },
            "location" : {
               "lat" : 17.2031945,
               "lng" : 77.8367282
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 17.701268,
                  "lng" : 78.8475038
               },
               "southwest" : {
                  "lat" : 16.8492337,
                  "lng" : 77.3753358
               }
            }
         },
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Andhra Pradesh",
               "short_name" : "AP",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Andhra Pradesh, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 19.916715,
                  "lng" : 84.80412939999999
               },
               "southwest" : {
                  "lat" : 12.596836,
                  "lng" : 76.749786
               }
            },
            "location" : {
               "lat" : 17.0477624,
               "lng" : 80.0981869
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 19.916715,
                  "lng" : 84.80412939999999
               },
               "southwest" : {
                  "lat" : 12.596836,
                  "lng" : 76.749786
               }
            }
         },
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 35.5044752,
                  "lng" : 97.395555
               },
               "southwest" : {
                  "lat" : 6.747138899999999,
                  "lng" : 68.1623859
               }
            },
            "location" : {
               "lat" : 20.593684,
               "lng" : 78.96288
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 35.5044752,
                  "lng" : 97.395555
               },
               "southwest" : {
                  "lat" : 6.747138899999999,
                  "lng" : 68.1627956
               }
            }
         },
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

ここで私の質問は、オブジェクトの下からこれらを取得する方法ですか?

> address components:
"long_name" : "Secunderabad",
"short_name" : "SC",
"long_name" : "Andhra Pradesh",
"short_name" : "AP",
"long_name" : "Andhra Pradesh",
"short_name" : "AP",
> location :
"lat" : 17.4251743,
"lng" : 78.5480111**

この形式のアドレスを取得できません。助けてください。前もって感謝します。

4

0 に答える 0