在单击对话框中添加时,标记不会显示

时间:2012-06-11 18:00:50

标签: android google-maps-markers itemizedoverlay customdialog

当用户点击地图时,我正试图在地图上添加标记 当我在onTouchEvent中添加标签位置标记时,地图会在标签处显示相应的标记,(标题和摘要的值是硬编码的)。
但问题是,当我想从用户那里获得title和snippet的值时,我已经创建了一个自定义提示对话框来输入详细信息。
当我将addOverlay()放在按钮的onclick事件处理程序中时,相应的标记不会显示 *使用AVD

以下代码可以正常使用

    public class MarkerOverlay extends ItemizedOverlay {

        private ArrayList<OverlayItem> locationOverlayItems = new ArrayList<OverlayItem>();
        private Context locationContext;
        private OverlayItem tabbedLocation;
        private AlertDialog promptDialog;
        private LayoutInflater inflater;
        private TextView info;
        private EditText title;
        private EditText description;

        public MarkerOverlay(Drawable defaultMarker, Context context) {
            super(boundCenterBottom(defaultMarker));
            locationContext = context;
        }   

        public void addOverlay(OverlayItem overlay) {
            locationOverlayItems.add(overlay);
            populate();
        }

        public void removeOverLay(OverlayItem overlay){
            locationOverlayItems.remove(overlay);
            populate();
        }

        @Override
        protected OverlayItem createItem(int arg0) {
            return locationOverlayItems.get(arg0);
        }

        @Override
        public int size() {
            return locationOverlayItems.size();
        }
        public boolean onTouchEvent(MotionEvent event, MapView mapView) {
            if (event.getAction() == 1) {
            int x = (int) event.getX();
            int y = (int) event.getY();
            final GeoPoint geoPoint = mapView.getProjection().fromPixels(x, y);

            tabbedLocation = new OverlayItem(geoPoint, "title","description");                      
            addOverlay(tabbedLocation);

            return false;
            }
       }
}  

这不是预期的结果

    public boolean onTouchEvent(MotionEvent event, MapView mapView) {
        if (event.getAction() == 1) {
            int x = (int) event.getX();
            int y = (int) event.getY();
            final GeoPoint geoPoint = mapView.getProjection().fromPixels(x, y);

            inflater = LayoutInflater.from(locationContext);
            View prompt = inflater.inflate(R.layout.marker_prompt, null);

            info = (TextView) prompt.findViewById(R.id.info);
            info.setText(geoPoint.getLatitudeE6() / 1E6 + "," + geoPoint.getLongitudeE6() / 1E6);

            title = (EditText) prompt.findViewById(R.id.title_text);
            description = (EditText) prompt.findViewById(R.id.description_text);

            promptDialog = new AlertDialog.Builder(locationContext)
            .setView(prompt)
            .setTitle("Add marker")
            .setMessage("Specify the details")
            .setPositiveButton("Add", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(!title.getText().toString().equals("") && !description.getText().toString().equals("")){
                        tabbedLocation = new OverlayItem(geoPoint, title.getText().toString(),
                                description.getText().toString());                      
                        addOverlay(tabbedLocation);
                    }
                }
            })
            .setNegativeButton("Cancel", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    promptDialog.cancel();
                }
            }).create();
            promptDialog.show();                
        }
        return false;
    }  

请帮忙解决 提前致谢

1 个答案:

答案 0 :(得分:0)

最后,我通过以下代码完成了这项工作,采用了稍微不同的方法,还有一些功能,例如在移动地图时禁用对话框。

    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_UP) {
            if(!moveMap){
                int x = (int) event.getX();
                int y = (int) event.getY();
                geoPoint = mapView.getProjection().fromPixels(x, y);

                final Dialog dialog = new Dialog(locationContext);
                dialog.setContentView(R.layout.marker_prompt);
                dialog.setTitle("Add Marker");

                info = (TextView) dialog.findViewById(R.id.info);
                info.setText(geoPoint.getLatitudeE6() / 1E6 + ","
                        + geoPoint.getLongitudeE6() / 1E6);

                title = (EditText) dialog.findViewById(R.id.title_text);
                description = (EditText) dialog
                        .findViewById(R.id.description_text);

                Button addButton = (Button) dialog.findViewById(R.id.add);
                Button cancelButton = (Button) dialog.findViewById(R.id.cancel);

                addButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (!title.getText().toString().equals("")
                                && !description.getText().toString()
                                        .equals("")) {
                            titleString = title.getText().toString();
                            snippetString = description.getText().toString();
                            tabbedLocation = new OverlayItem(geoPoint, titleString, snippetString); 
                            addOverlay(tabbedLocation);
//                          Log.d(titleString, snippetString);
                            dialog.dismiss();
                            LocationApplication.getInstance(
                                    locationContext).saveMarker(
                                    geoPoint, tabbedLocation);
                        }
                    }
                });

                cancelButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }
        } else if (action == MotionEvent.ACTION_DOWN) {
            moveMap = false;

        } else if (action == MotionEvent.ACTION_MOVE) {
            moveMap = true;
        }
        return false;
    }

我在这里使用Dialog而不是警告对话框setContentView()而是使布局膨胀。我还不清楚为什么以前的方法不起作用。

无论如何,感谢所有刚看过这个问题的人:P