无法使用反向地理编码显示地址

时间:2011-05-12 07:03:42

标签: android

这是我的代码......

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
            TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
            TextView myAddress = (TextView)findViewById(R.id.myaddress);

            myLatitude.setText("Latitude: " + String.valueOf(LATITUDE));
            myLongitude.setText("Longitude: " + String.valueOf(LONGITUDE));

            Geocoder geocoder = new Geocoder(this, Locale.getDefault());

            try {
                //List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
                /* List<Address> addresses = new Geocoder(this,Locale.getDefault()).getFromLocation(LATITUDE, LONGITUDE, 1);

                if(addresses != null) {
                    Address returnedAddress = addresses.get(0);
                    StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
                    for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                    }
                    myAddress.setText(strReturnedAddress.toString());*/

                List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1); 
                StringBuilder sb = new StringBuilder(); 
                if (addresses.size() > 0) { 
                   Address address = addresses.get(0); 

                   for (int i = 0; i < address.getMaxAddressLineIndex(); i++) 
                      addr=address.getAddressLine(i); 
                }
                else{
                    myAddress.setText("No Address returned!");
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                /*e.printStackTrace();
                myAddress.setText("Canont get Address!");*/
            }

        }

任何人都可以解决问题是什么??? 提前谢谢。

2 个答案:

答案 0 :(得分:0)

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
            TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
            TextView myAddress = (TextView)findViewById(R.id.myaddress);

            myLatitude.setText("Latitude: " + String.valueOf(LATITUDE));
            myLongitude.setText("Longitude: " + String.valueOf(LONGITUDE));

            Geocoder geocoder = new Geocoder(this, Locale.getDefault());

            try {
                //List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
                /* List<Address> addresses = new Geocoder(this,Locale.getDefault()).getFromLocation(LATITUDE, LONGITUDE, 1);

                if(addresses != null) {
                    Address returnedAddress = addresses.get(0);
                    StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
                    for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                    }
                    myAddress.setText(strReturnedAddress.toString());*/

                List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1); 
                StringBuilder sb = new StringBuilder(); 
                if (addresses.size() > 0) { 
                   Address address = addresses.get(0); 

                   for (int i = 0; i < address.getMaxAddressLineIndex(); i++) 
                      sb.append(address.getAddressLine(i)+ ",");

                }
                else{
                    myAddress.setText("No Address returned!");
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                /*e.printStackTrace();
                myAddress.setText("Canont get Address!");*/
            }
                  myAddress.setText(sb.toString());
        }

答案 1 :(得分:0)

我建议您首先检查GeoCoder实现的存在,然后再使用它。它不一定存在于所有设备中。

  

Geocoder类需要后端   服务不包括在内   核心android框架。地理编码器   查询方法将返回一个空的   如果没有后端服务,请列出   该平台。使用isPresent()   确定是否为Geocoder的方法   实施存在。