-3

我正在尝试一个简单的应用程序,我需要在其中显示地图。当我点击任何位置时,它应该返回该位置的地址。你能指导我如何进行此操作吗?

4

3 回答 3

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;

    }
于 2012-05-23T13:27:26.750 回答
0

你知道如何在你的安卓应用中显示地图吗?如果没有,你可以看看这个不错的教程。

然后显示地址可以这样完成:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
于 2012-05-23T11:42:50.160 回答
0

要获取单击的坐标,请检查此线程:

单击 MapView 上的任意位置时获取坐标

您可以使用以下命令将坐标转换为地址:

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

这会对 google 服务器进行 http 调用,因此您可能希望将其放入某种线程中。

于 2012-05-23T11:42:54.440 回答