【问题标题】:Geocoding example not working地理编码示例不起作用
【发布时间】:2012-07-11 10:31:40
【问题描述】:

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

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();
                }
            }
        });

    }
}

【问题讨论】:

  • 首先,您没有初始化 Geocoder 对象...请在此处发布堆栈跟踪,如果您需要更多帮助,还可以提高您的接受率:)
  • 您是在真机还是模拟器上测试?因为Geocoder 不能在模拟器上运行
  • @AditiSharma 请看我的回答。
  • 是的,我看到了你的答案。现在可以了。

标签: android geocoding


【解决方案1】:

请写下面的代码

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

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

【讨论】:

    【解决方案2】:

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

    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”= 纬度

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多