0

我编写了一个简单的地理编码应用程序,但它不起作用。我已授予所有必要的权限。请有人告诉我哪里出错了。提前谢谢。

public class ForgeocdingActivity extends Activity {
    Geocoder gc;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText ed=(EditText)findViewById(R.id.editText1);
        Button b1=(Button)findViewById(R.id.button1);
        final String  to_add = ed.getText().toString();    
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) 
            {
                try
                {
                List<Address> address2 = gc.getFromLocationName(to_add,3);

                if(address2 != null && address2.size() > 0)
                {
                         double lat1 = address2.get(0).getLatitude();
                         double lng1 = address2.get(0).getLongitude();
                         Toast.makeText(getBaseContext(), "Lat:"+lat1+"Lon:"+lng1, Toast.LENGTH_SHORT).show();
                }      
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        });

    }
}
4

3 回答 3

1

请为此编写以下代码

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

现在地址列表包含最近的已知区域,并将此代码测试到真实设备中。

于 2012-07-11T10:58:26.870 回答
0

这是地理编码的工作示例:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
  try {
        List<Address> list = geocoder.getFromLocation(lat,long, 1);
        String address = list.get(0).getAddressLine(0) + ", " + list.get(0).getAddressLine(1) + ", " + list.get(0).getAddressLine(2);               
  } catch (IOException e) {
     e.printStackTrace();
  }

其中“this”= 活动上下文,“long”= 经度,“lat”= 纬度

于 2012-07-11T10:43:17.697 回答
0

我认为您可以尝试从 address1 获取纬度和经度。然后您将从以下代码中获取纬度和经度 Listaddress = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

于 2012-07-11T11:30:10.267 回答