如何在Android中获取纬度和经度传递密码

时间:2015-10-26 06:57:49

标签: android google-maps

如何通过将参数作为密码传递来获取经度和经度。我有带pincode的表格。当在文本框中输入一些密码时,如何获取经纬度。但是不通过地址如何获取经纬度只作为参数传递pincode。

尝试这样做不起作用。任何人帮助我

的java

final Geocoder geocoder = new Geocoder(getActivity());
                    pincode = pincode1.getText().toString();
                    try {
                        List<Address> addresses = geocoder.getFromLocationName(pincode, 1);
                        if (addresses != null && !addresses.isEmpty()) {
                            Address address = addresses.get(0);
                            // Use the address as needed
                            String message = String.format("Latitude: %f, Longitude: %f",
                                    address.getLatitude(), address.getLongitude());
                            Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
                        } else {
                            // Display appropriate message when Geocoder services are not available
                            Toast.makeText(getActivity(), "Unable to geocode zipcode", Toast.LENGTH_LONG).show();
                        }
                    } catch (IOException e) {
                        // handle exception
                    }

1 个答案:

答案 0 :(得分:0)

试试这个......

您可以http Get request for the following link

http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:638701&sensor=false

演示结果:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "638701",
               "short_name" : "638701",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Tamil Nadu",
               "short_name" : "TN",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Tamil Nadu 638701, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 11.12035,
                  "lng" : 77.7041998
               },
               "southwest" : {
                  "lat" : 10.9142493,
                  "lng" : 77.4507146
               }
            },
            "location" : {
               "lat" : 11.0179181,
               "lng" : 77.58486649999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 11.12035,
                  "lng" : 77.7041998
               },
               "southwest" : {
                  "lat" : 10.9142493,
                  "lng" : 77.4507146
               }
            }
         },
         "place_id" : "ChIJl35xdEKfqTsRcr8NKDs37OM",
         "types" : [ "postal_code" ]
      }
   ],
   "status" : "OK"
}

根据您的要求更改邮政编码元素

https://developers.google.com/maps/documentation/geocoding/intro?csw=1#JSON