如何在listview中使用地理编码来显示地图

时间:2014-06-02 14:14:13

标签: android listview google-geocoder

我有一个地址列表视图,如果按任意一行,它应该在地图中显示特定地址。为此,我使用Geoocder获取该地址的纬度和经度。

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

      lvEmployees = (ListView) findViewById(R.id.lvEmployees);


      lvEmployees.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View v,
                int position, long id) {

            String address;
            Geocoder coder = new Geocoder(getApplicationContext());
            List<Address> address1;


            // If Google Play Services is available


            // Display Selected Row of Listview into EditText widget

            Cursor row = (Cursor) adapter.getItemAtPosition(position);
            try {
                String strAddress=row.getString(0)+" "+row.getString(2);
                System.out.println("strAddress>>>"+strAddress);
                address1 = coder.getFromLocationName(strAddress,5);

                Address location = address1.get(0);

                latitude= location.getLatitude();
                longitude=  location.getLongitude();

            }
            catch(Exception e){
            }
            System.out.println(latitude+" "+longitude);
            String url = "http://maps.google.com/maps?daddr="+ latitude + ","+longitude;
            Intent geoIntent = new Intent(
                    android.content.Intent.ACTION_VIEW,Uri.parse(url));
            startActivity(geoIntent);
            System.out.println("url>>"+url);

        }

    }

问题在于我获得相同的纬度和纬度值的所有地址。

请提供一些解决方案。

1 个答案:

答案 0 :(得分:0)

问题是你只有一个你的地址实例,所以它需要给出最后一个值 您需要做的是将声明地址数据放在listview ArraysAdapter类中,并在需要时将其作为参数发送