0

当用户单击地图时,我试图在地图上添加一个标记,
当我在 onTouchEvent 中添加选项卡式位置标记时,地图会在选项卡式位置显示相应的标记,(标题和片段的值是硬编码的) .
但是,问题是,当我应该从用户那里获取标题和片段的值时,我创建了一个自定义提示对话框来输入详细信息。
当我将 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;
    }  

请帮忙解决
提前谢谢

4

1 回答 1

0

Finally i got it done by the following code with a slightly different approach, with some more functionality such as disabling the dialog while moving the map.

    @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;
    }

Here i'm using Dialog instead alert dialog, setContentView() instead inflate the layout. Still i'm not clear why the previous approach did not work.

Anyway, thanks for everyone who just viewed this question :P

于 2012-08-04T03:10:34.710 回答