android - 按 ListViewItem 时谷歌地图的错误协调

标签 android json google-maps android-listview imagebutton

我有一个自定义的 Listview,每个项目上都有一个图像按钮,它应该给我指向建筑物的方向。当按下 DirectionsButtons 时,谷歌地图打开,但坐标填写错误。 到目的地,它只给了我一个逗号。有人可以帮我找出我做错了什么吗?

public class buildingList extends Activity {

    String myLat = "";
    String myLng = "";
    private List<buildingObject> buildingItem = new ArrayList<buildingObject>();
    ViewHolder holder = new ViewHolder();
    ListView list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLocations();
        setContentView(R.layout.building_list);

        new GetbuildingsTask().execute(myLat, myLng);

    }

    private void populateListview() {
        ArrayAdapter<buildingObject> adapter = new MyListAdapter();
        list = (ListView) findViewById(R.id.buildingList);
        list.setAdapter(adapter);
    }

    private class MyListAdapter extends ArrayAdapter<buildingObject> {
        public MyListAdapter() {
            super(buildingList.this, R.layout.building_row, buildingItem);
        }

        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {
            //Null possible, so checking
            View itemView = convertView;
            if (itemView == null) {
                itemView = getLayoutInflater().inflate(R.layout.building_row, parent, false);
            }

            // Find the item to work with
            buildingObject currentItem = buildingItem.get(position);

            // Filling the View         
            holder.thumbnail = (ImageButton) itemView.findViewById(R.id.btnbuildingListThumbnail);
            holder.thumbnail.setBackgroundResource(currentItem.getThumbnail());

            holder.name = (TextView) itemView.findViewById(R.id.lblbuildingListItemName);
            holder.name.setText(currentItem.getName());
            holder.btnDirections = (ImageButton) itemView.findViewById(R.id.btnbuildingListDirections);
            holder.thumbnail = (ImageButton) itemView.findViewById(R.id.btnbuildingListThumbnail);
            holder.btnShare = (ImageButton) itemView.findViewById(R.id.btnbuildingListShare);

            holder.btnDirections.setOnClickListener(new OnClickListener() {             
                @Override
                public void onClick(View v) {               
                    TextView txtLat = (TextView) parent.getChildAt(position - list.getFirstVisiblePosition()).findViewById(R.id.lblbuildingListlatitude);
                    TextView txtLng = (TextView) parent.getChildAt(position - list.getFirstVisiblePosition()).findViewById(R.id.lblbuildingListlongitude);
                    String lat = txtLat.getText().toString();
                    String lng = txtLng.getText().toString();
                    Log.v("Lat ", "Lat of selected building is " + lat);
                    Log.v("Lng ", "Lng of selected building is " + lng);

                    Uri uri = Uri.parse("http://maps.google.com/maps?&saddr=" + myLat + "," + myLng + "&daddr=" + lat + "," + lng);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);  
                }
            });

            holder.thumbnail.setOnClickListener(new OnClickListener(){
                public void onClick(View v) {                       
                    String btnmsg = "ThumbNail";
                    Toast.makeText(buildingList.this, btnmsg, Toast.LENGTH_LONG).show();                        
                }
            });

            holder.btnShare.setOnClickListener(new OnClickListener() {              
                @Override
                public void onClick(View v) {               
                    String btnmsg = "Share";
                    Toast.makeText(buildingList.this, btnmsg, Toast.LENGTH_LONG).show();                        
                }
            });

            return itemView;
        }
    }

    static class ViewHolder {
        ImageButton thumbnail;
        TextView name;
        ImageButton btnDirections;
        ImageButton btnShare;
    }

    private void getLocations() {
        String[] locations = getApplicationContext().fileList();
        FileInputStream fis;

        for (int i = 0; i < locations.length; i++) {
            if (locations[i].equals("my_latitude")) {
                try {
                    fis = openFileInput(locations[i]);
                    byte[] input = new byte[fis.available()];
                    while (fis.read(input) != -1) {
                        myLat += new String(input);
                    }
                    fis.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (locations[i].equals("my_longitude")) {
                try {
                    fis = openFileInput(locations[i]);
                    byte[] input = new byte[fis.available()];
                    while (fis.read(input) != -1) {
                        myLng += new String(input);
                    }
                    fis.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    class GetbuildingsTask extends AsyncTask<String, Void, JSONArray> {

        @Override
        protected JSONArray doInBackground(String... data) {
            String lat = data[0];
            String lng = data[1];

            JSONParser jParser = new JSONParser();
            JSONArray json = jParser
                    .getJSONFromUrl("http://www.mysite.eu/index.php/building/searchbuildingsJSON?lat=" + lat + "&lng=" + lng + "&radius=10");
            return json;
        }

        protected void onPostExecute(JSONArray result) {

            if (result != null) {

                try {
                    Log.v(result.getString(0), result.getString(0));

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Toast toast = Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_SHORT);
                toast.show();
                Log.d("whut", "" + result.length());

                String[] buildings = new String[result.length()];
                buildingObject[] building = new buildingObject[result.length()];
                try {
                    for (int i = 0; i < result.length(); i++) {
                        JSONObject row;
                        row = result.getJSONObject(i);
                        buildings[i] = row.getString("Name");
                        buildings[i] += "" + row.getDouble("Latitude");
                        buildings[i] += "" + row.getDouble("Longitude");
                        buildings[i] += ". Distance: " + row.getDouble("distance") + "km";


                        String Name = row.getString("Name");
                        float lat = (float) row.getDouble("Latitude");
                        float lng = (float) row.getDouble("Longitude");
                        float dist = (float) row.getDouble("distance");

                        //building[i] = new buildingObject(Name, lat, lng, dist);
                        buildingItem.add(new buildingObject(Name,lat, lng, dist));

                    }
                } catch (JSONException e) {
                    Log.v("Noooooooooooo", "You were the chosen one");
                    Log.e("Noooooooooooo", "Dis iz die erreur: " + e.toString());
                }

                populateListview();

            } else {
                Toast toast = Toast.makeText(getApplicationContext(),
                        "Please enable internet", Toast.LENGTH_LONG);
                toast.show();
            }
        }
    }
}

最佳答案

您的代码看起来不错。

您是否将调试点设置为:

Uri uri = Uri.parse("http://maps.google.com/maps?&saddr=" + myLat + "," + myLng + "&daddr=" + lat + "," + lng)

我的猜测是 myLat 和 myLng 是空的,因此只留下一个“,”(逗号)。

如果它实际上是空的或 null,那么我要放置调试点的下一个位置是(或之前):

myLat += new String(input);

myLng += new String(input);

这是被击中了吗?是否抛出异常?

编辑

附近:

// Filling the View

您在哪里填充 lblbuildingLiSTLatitude 和长 TextView ?

关于android - 按 ListViewItem 时谷歌地图的错误协调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17014483/

相关文章:

java - TrackballGestureDetector 实例化

java - 将 24 位位图数据正确加载到 32 位位图对象中

android - 如何在邮件正文中插入新行

php - jquery json循环数据 - 只是无法弄清楚这一点

javascript - 将鼠标悬停在圆形、多边形或矩形上

javascript - 如何在 PhoneGap 的 windows 平台上实现 map (Google 或 Bing)?

android - 用于缓存图像的 AQuery 和 Volley ImageLoader

java - Jackson 数据绑定(bind)与异构 Json 对象

java - 尝试将 JSON 字符串转换为 JavaPOJO 时遇到奇怪的问题

通过关键字访问 Python 字典(TypeError : list indices must be integers or slices, 不是 str)