地理编码和反向地理编码

时间:2012-05-23 11:36:49

标签: android google-geocoder

我正在尝试一个简单的应用程序,我需要显示一个地图。当我点击任何位置时,它应该返回该位置的地址。请你指导我如何继续这个。

3 个答案:

答案 0 :(得分:6)

我发现Google API对于反向地​​理编码非常方便。为此,我创建了这个课程。您可以在应用中直接使用此课程:

public class getReverseGeoCoding
{
    private String Address1 = "", Address2 = "", City = "", State = "", Country = "", County = "", PIN = "";

    public void getAddress()
    {
        Address1 = "";  Address2 = ""; City = ""; State = ""; Country = ""; County = ""; PIN = "";

        try {

            JSONObject jsonObj = parser_Json.getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng="+ Global.curLatitude + "," + Global.curLongitude +"&sensor=true"); 
            String Status = jsonObj.getString("status");
            if(Status.equalsIgnoreCase("OK"))
            {
                JSONArray Results = jsonObj.getJSONArray("results"); 
                JSONObject zero = Results.getJSONObject(0);
                JSONArray address_components = zero.getJSONArray("address_components");

                for(int i = 0; i<address_components.length(); i++)
                {
                    JSONObject zero2 = address_components.getJSONObject(i);
                    String long_name = zero2.getString("long_name");
                    JSONArray mtypes = zero2.getJSONArray("types");
                    String Type = mtypes.getString(0);

                    if(TextUtils.isEmpty(long_name) == false || !long_name.equals(null) || long_name.length() > 0 || long_name != "" )
                    {
                        if(Type.equalsIgnoreCase("street_number"))
                        {
                            Address1 = long_name + " ";
                        }
                        else if(Type.equalsIgnoreCase("route"))
                        {
                            Address1 = Address1 + long_name;
                        }
                        else if(Type.equalsIgnoreCase("sublocality"))
                        {
                            Address2 = long_name;
                        }
                        else if(Type.equalsIgnoreCase("locality"))
                        {
//                          Address2 = Address2 + long_name + ", ";
                            City = long_name;
                        }
                        else if(Type.equalsIgnoreCase("administrative_area_level_2"))
                        {
                            County = long_name;
                        }
                        else if(Type.equalsIgnoreCase("administrative_area_level_1"))
                        {
                            State = long_name;
                        }
                        else if(Type.equalsIgnoreCase("country"))
                        {
                            Country = long_name;
                        }
                        else if(Type.equalsIgnoreCase("postal_code"))
                        {
                            PIN = long_name;
                        }
                    }

//                  JSONArray mtypes = zero2.getJSONArray("types");
//                  String Type = mtypes.getString(0);
//                  Log.e(Type,long_name);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public String getAddress1()
    {
        return Address1;

    }

    public String getAddress2()
    {
        return Address2;

    }

    public String getCity()
    {
        return City;

    }

    public String getState()
    {
        return State;

    }

    public String getCountry()
    {
        return Country;

    }

    public String getCounty()
    {
        return County;

    }

    public String getPIN()
    {
        return PIN;

    }

答案 1 :(得分:0)

您知道如何在Android应用中显示地图吗?如果没有,你可以看看this漂亮的教程。

然后显示地址可以这样做:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);

答案 2 :(得分:0)

获取点击的坐标,请检查此主题:

Getting coordinates when clicking anywhere on a MapView

您可以使用以下方法将坐标转换为地址:

 Address a = ((Address)geocoder.getFromLocation(latiAsDouble, longiAsDouble, 1).get(0));
 String Position = a.getAddressLine(1) + " - " + a.getAddressLine(0);

这是对谷歌服务器的http调用,所以你可能想把它放到某种线程中。