Search code examples
androidgoogle-maps-markersitemizedoverlaycustomdialog

Marker not getting displayed when adding within a dialog box onclick


I'm trying to add a marker on the map when user click on the map,
When i add the tabbed location marker within the onTouchEvent the map showing the corresponding marker at the tabbed place, (The values of title and snippet are hard coded).
But, the problem is, when i supposed to get the values of title and snippet from user i've created a custom prompt dialog to enter details.
The corresponding marker not getting displayed when i put addOverlay() within a button's onclick event handler.
*Using AVD

The following code works fine

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

This is not giving expected result

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

please help to solve
Thanks in advance


Solution

  • 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