简单的地理编码示例

时间:2011-02-28 06:33:16

标签: android geocoding

我正在实施一个简单的地理编码示例,用户输入地址并获取其经度和纬度。

                addr = Area_edtxt.getText().toString();
                try {
                    list_addr = gc.getFromLocationName(addr, 1);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.d("Location lookup failed", e.getMessage());
                }
                if (list_addr != null && list_addr.size() > 0 ){
                     latitude = list_addr.get(0).getLatitude();
                     longitude = list_addr.get(0).getLongitude();
                     latitude_edtxt.setText(latitude.toString());
                     longitude_edtxt.setText(longitude.toString());
                }else {
                    latitude_edtxt.setText("Address not found");
                }

但显示错误:无法打开堆栈跟踪文件'/data/anr/traces.txt':权限被拒绝。

1 个答案:

答案 0 :(得分:1)

要快速尝试,您可以使用AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html

private class GeocodeTask extends AsyncTask<String, Integer, List> {
 protected Long doInBackground(String... address) {
  try {
         return gc.getFromLocationName(address[0], 1)
      } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.d("Location lookup failed", e.getMessage());
    }
 }

 protected void onPostExecute(List result) {
     if (list_addr != null && list_addr.size() > 0 ){
         latitude = list_addr.get(0).getLatitude();
         longitude = list_addr.get(0).getLongitude();
         latitude_edtxt.setText(latitude.toString());
         longitude_edtxt.setText(longitude.toString());
     }else {
         latitude_edtxt.setText("Address not found");
     }
 }

}

new GeocodeTask().execute(addr);
相关问题