android - Add marker to map on tap -


i'm coding application using osmdroid show map , add marker when user taps on map, have been able usin motion event adds marker when user zooming in or out map don't want this. code have add marker:

@override         public boolean dispatchtouchevent(motionevent ev) {         int actiontype = ev.getaction();         switch (actiontype) {         case motionevent.action_down:                 projection proj = myopenmapview.getprojection();                 geopoint loc = (geopoint) proj.frompixels((int)ev.getx(), (int)ev.gety());                  string longitude = double.tostring(((double)loc.getlongitudee6())/1000000);                 string latitude = double.tostring(((double)loc.getlatitudee6())/1000000);                 list<overlayitem> anotheroverlayitemarray = new arraylist<overlayitem>();                 extendedoverlayitem mapitem = new extendedoverlayitem("", "", new geopoint((((double)loc.getlatitudee6())/1000000), (((double)loc.getlongitudee6())/1000000)), this);                 mapitem.setmarker(this.getresources().getdrawable(r.drawable.marker));                 anotheroverlayitemarray.add(mapitem);                 itemizediconoverlay<overlayitem> anotheritemizediconoverlay                   = new itemizediconoverlay<overlayitem>(                    this, anotheroverlayitemarray, null);                 myopenmapview.getoverlays().add(anotheritemizediconoverlay);                 toast toast = toast.maketext(getapplicationcontext(), "longitude: "+ longitude +" latitude: "+ latitude , toast.length_long);                 toast.show();          }         return super.dispatchtouchevent(ev);     } 

this how solved it:

overlay touchoverlay = new overlay(this){                     itemizediconoverlay<overlayitem> anotheritemizediconoverlay = null;                      @override                     protected void draw(canvas arg0, mapview arg1, boolean arg2) {                      }                     @override                     public boolean onsingletapconfirmed(final motionevent e, final mapview mapview) {                         projection proj = mapview.getprojection();                         geopoint loc = (geopoint) proj.frompixels((int)e.getx(), (int)e.gety());                          longitude = double.tostring(((double)loc.getlongitudee6())/1000000);                         latitude = double.tostring(((double)loc.getlatitudee6())/1000000);                         arraylist<overlayitem> overlayarray = new arraylist<overlayitem>();                         overlayitem mapitem = new overlayitem("", "", new geopoint((((double)loc.getlatitudee6())/1000000), (((double)loc.getlongitudee6())/1000000)));                         mapitem.setmarker(marker);                         overlayarray.add(mapitem);                         if(anotheritemizediconoverlay==null){                             anotheritemizediconoverlay = new itemizediconoverlay<overlayitem>(getapplicationcontext(), overlayarray,null);                             myopenmapview.getoverlays().add(anotheritemizediconoverlay);                             myopenmapview.invalidate();                         }else{                             myopenmapview.getoverlays().remove(anotheritemizediconoverlay);                             myopenmapview.invalidate();                             anotheritemizediconoverlay = new itemizediconoverlay<overlayitem>(getapplicationcontext(), overlayarray,null);                             myopenmapview.getoverlays().add(anotheritemizediconoverlay);                         }                         dlgthread();                         return true;                     }                 };                 myopenmapview.getoverlays().add(touchoverlay); 

you should create overlay , override onsingletapconfirmed() method single-taps:

@override public boolean onsingletapconfirmed(final motionevent event, final mapview mapview) {     // handle single-tap here, return true.     return true; } 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -