How to dismiss AlertDialog.Builder on touch with no buttons - android

When my alert pops up I want it to dismiss when it is touched again with out adding any buttons to it.
so on a map pin touch
#Override
protected boolean onTap(int i) {
OverlayItem item = items.get(i);
dialog.setTitle(item.getSnippet());
dialog.setMessage(item.getTitle());
dialog.show();
return (true);
}
So how do I make it go away by touching any where in the alert or the background.
EDIT:ADDED CODE
private class SitesOverlay extends ItemizedOverlay<CustomItem> {
private Drawable heart=null;
private List<CustomItem> items=new ArrayList<CustomItem>();
private PopupPanel panel=new PopupPanel(R.layout.popup);
public SitesOverlay() {
super(null);
heart=getMarker(R.drawable.heart_full);
JSONObject json;
JSONObject json2;
String resultURL = queryRESTurl("http://www.kickintheapp.com/api/maps393/json.json");
try {
json = new JSONObject(resultURL);
JSONArray nameArray = json.names();
JSONArray valArray;
valArray = json.toJSONArray(nameArray);
for (int i = 0; i < valArray.length(); i++)
{
Log.e(nameArray.getString(i), valArray.getString(i));
json2 = new JSONObject(valArray.getString(i));
JSONArray nameArray2 = json2.names();
JSONArray valArray2 = json2.toJSONArray(nameArray2);
for (int a = 0; a < valArray2.length(); a++)
{
// add to maps here
items.add(new CustomItem(getPoint(valArray2.getDouble(3),valArray2.getDouble(2)), valArray2.getString(1), valArray2.getString(0),getMarker(R.drawable.pin_yellow), heart));
Log.e(nameArray2.getString(a), valArray2.getString(a));
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
populate();
}
#Override
protected CustomItem createItem(int i) {
return(items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
}
#Override
protected boolean onTap(int i) {
OverlayItem item=getItem(i);
GeoPoint geo=item.getPoint();
Point pt=map.getProjection().toPixels(geo, null);
View view=panel.getView();
((TextView)view.findViewById(R.id.latitude))
.setText(item.getSnippet());
((TextView)view.findViewById(R.id.longitude))
.setText(item.getTitle());
panel.show(pt.y*2>map.getHeight());
return(true);
}
class PopupPanel {
View popup;
boolean isVisible=false;
PopupPanel(int layout) {
ViewGroup parent=(ViewGroup)map.getParent();
popup=getLayoutInflater().inflate(layout, parent, false);
popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hide();
}
});
}
View getView() {
return(popup);
}
void show(boolean alignTop) {
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
if (alignTop) {
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.setMargins(0, 20, 0, 0);
}
else {
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.setMargins(0, 0, 0, 60);
}
hide();
((ViewGroup)map.getParent()).addView(popup, lp);
isVisible=true;
}
void hide() {
if (isVisible) {
isVisible=false;
((ViewGroup)popup.getParent()).removeView(popup);
}
}
}

Any particular reason not to use PopupWindow? It allows you being notified for "inside" and "outside" touch events very easily.

Related

Adding infoWindow on top of marker Google Map

I want to add an info window on top of Each marker set when user tap on a marker. Here is my code. The marker is fine i need only the infowindow appears on top of it. how can i achieve this, here is my code
public class MainActivity extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
Drawable makerDefault = this.getResources().getDrawable(R.drawable.marker_default);
MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(makerDefault);
Drawable windmill = getResources().getDrawable(R.drawable.windmill);
Drawable bigBen = getResources().getDrawable(R.drawable.big_ben);
Drawable eiffelTower = getResources().getDrawable(R.drawable.eiffel_tower);
itemizedOverlay.addOverlayItem(52372991, 4892655, "Amsterdam", windmill);
itemizedOverlay.addOverlayItem(51501851, -140623, "London", bigBen);
itemizedOverlay.addOverlayItem(48857522, 2294496, "Paris", eiffelTower);
mapView.getOverlays().add(itemizedOverlay);
MapController mc = mapView.getController();
mc.setCenter(new GeoPoint(51035349, 2370987)); // Dunkerque, Belgium
mc.zoomToSpan(itemizedOverlay.getLatSpanE6(), itemizedOverlay.getLonSpanE6());
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
private class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlayItem(int lat, int lon, String title, Drawable altMarker) {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem, altMarker);
}
public void addOverlayItem(OverlayItem overlayItem) {
mOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(OverlayItem overlayItem, Drawable altMarker) {
overlayItem.setMarker(boundCenterBottom(altMarker));
addOverlayItem(overlayItem);
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(int index) {
Toast.makeText(MainActivity.this, getItem(index).getTitle(), Toast.LENGTH_LONG).show();
return true;
}
}
}
SitesOverlay.class
private class SitesOverlay extends ItemizedOverlay<CustomItem> {
private List<CustomItem> items;
private View view = null;
public SitesOverlay() {
super(null);
items = new ArrayList<CustomItem>();
items.add(new CustomItem(pt, busName, "Bendigo", marker));
boundCenter(marker);
populate();
}
#Override
protected CustomItem createItem(int i) {
return (items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (!shadow) {
super.draw(canvas, mapView, false);
}
}
#Override
public int size() {
return (items.size());
}
public void refresh() {
populate();
}
public void clear() {
items.clear();
resetLastFocuesIndex();
}
public void resetLastFocuesIndex() {
setLastFocusedIndex(-1);
}
#Override
protected boolean onTap(final int index) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().removeView(view);
getMapView().invalidate();
view = null;
}
view = getLayoutInflater().inflate(R.layout.balloon_overlay, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.balloon_main_layout);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setBackgroundResource(R.drawable.balloon_overlay_bg_selector);
ImageView image = (ImageView) view
.findViewById(R.id.balloon_disclosure);
TextView text = (TextView) view
.findViewById(R.id.balloon_item_title);
text.setText(items.get(index).getTitle());
Projection projection = getMapView().getProjection();
Point point = new Point();
projection.toPixels(items.get(index).getPoint(), point);
int x = (int) (view.getWidth() / 2f);
int y = -marker.getIntrinsicHeight() - 3;
MapView.LayoutParams lp = new MapView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, items.get(index)
.getPoint(), x, y + 50,
MapView.LayoutParams.BOTTOM_CENTER);
getMapView().removeView(view);
getMapView().invalidate();
getMapView().addView(view, lp);
getMapView().invalidate();
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (!items.isEmpty()) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().invalidate();
// Intent intent=new Intent(MapLocationActivity.this,);
}
}
getMapView().invalidate();
}
});
return true;
}
}
class CustomItem extends OverlayItem {
Drawable marker = null;
CustomItem(GeoPoint pt, String name, String snippet, Drawable marker) {
super(pt, name, snippet);
this.marker = marker;
}
#Override
public Drawable getMarker(int stateBitset) {
Drawable result = marker;
setState(result, stateBitset);
return (result);
}
}
set Overlay mapview.getOverlays().add(new SitesOverlay());
if there is multiple marker then
for (int i = 0; i < arrayList.size(); i++) {
slat = Double.parseDouble(arrayList.get(i).getLat());
vlong = Double.parseDouble(arrayList.get(i).getvLong());
pt = new GeoPoint((int) (slat * 1E6), (int) (vlong * 1E6));
Log.e("lat long", "--- "+slat);
MapviewActivity.this.mc.animateTo(pt);
items.add(new CustomItem(pt, arrayList.get(i).getBuissnessName(), "Bendigo", marker));
boundCenter(marker);
}

Pop up menu android on google map application

I am doing my graduation thesis and I have to do an Android application which shows my location on Google Maps (something I have done), and when I click some pins in the university will pop up some informations for the buildings and if I could when i click them they will redirect to a url.I have done it with Toast but it doesn't fullfill my needs.I have searched and found NooYawk pop up.Here is my code if anyone can help I will be grateful.
Ps I am amateur
public class TGpsActivity extends MapActivity {
MapView mapView=null;
MapController mapController=null;
MyLocationOverlay whereAmI=null;
#Override
protected boolean isLocationDisplayed(){
return whereAmI.isMyLocationEnabled();
}
#Override
protected boolean isRouteDisplayed(){
return true;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.pushpin);
marker.setBounds((int) (-marker.getIntrinsicWidth()/2),-marker.getIntrinsicHeight(),(int) (marker.getIntrinsicWidth()/2),0);
mapController=mapView.getController();
mapController.setZoom(15);
InterestingLocations funPlaces= new InterestingLocations(marker);
mapView.getOverlays().add(funPlaces);
GeoPoint pt=funPlaces.getCenterPt();
int latSpan=funPlaces.getLatSpanE6();
int lonSpan=funPlaces.getLonSpanE6();
Log.v("Overlays", "Lat span is " + latSpan);
Log.v("Overlays", "Lon span is " + lonSpan);
MapController mc = mapView.getController();
mc.setCenter(pt);
mc.zoomToSpan((int) (latSpan*1.5), (int)(lonSpan*1.5));
whereAmI= new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(whereAmI);
mapView.postInvalidate();
}
public class InterestingLocations extends ItemizedOverlay{
private ArrayList<OverlayItem> locations = new ArrayList<OverlayItem>();
private GeoPoint center=null;
public InterestingLocations(Drawable marker){
super(marker);
GeoPoint headOffice = new GeoPoint((int)(37.97897823618044*1000000) ,(int)(23.6723855137825*1000000));
GeoPoint computerRoom= new GeoPoint((int)(37.98013047446126*1000000) ,(int)(13.6715030670166*1000000));
locations.add(new OverlayItem(headOffice,"headOffice","headOffice"));
locations.add(new OverlayItem(computerRoom,"computerRoom","computerRoom"));
populate();
}
public GeoPoint getCenterPt(){
if(center==null){
int northEdge= -90000000;
int southEdge= 90000000;
int eastEdge= -180000000;
int westEdge= 180000000;
Iterator<OverlayItem> iter=locations.iterator();
while(iter.hasNext()){
GeoPoint pt=iter.next().getPoint();
if(pt.getLatitudeE6() > northEdge)
northEdge=pt.getLatitudeE6();
if(pt.getLatitudeE6() < southEdge)
southEdge=pt.getLatitudeE6();
if(pt.getLongitudeE6() > eastEdge)
eastEdge=pt.getLongitudeE6();
if(pt.getLongitudeE6() < westEdge)
westEdge=pt.getLongitudeE6();
}
center=new GeoPoint((int)((northEdge+southEdge)/2),(int)((westEdge+eastEdge)/2));
}
return center;
}
#Override
public void draw(Canvas canvas, MapView mapView,boolean shadow)
{
super.draw(canvas, mapView, shadow);
}
#Override
protected OverlayItem createItem(int i){
return locations.get(i);
}
#Override
public int size() {
return locations.size();
}
#Override
protected boolean onTap(int i) {
Toast.makeText(TGpsActivity.this,
locations.get(i).getSnippet(),
Toast.LENGTH_LONG).show();
return(true);
}
}
#Override
public void onResume(){
super.onResume();
whereAmI.enableMyLocation();
whereAmI.runOnFirstFix(new Runnable(){
public void run(){
mapController.setCenter(whereAmI.getMyLocation());
}
});
}
#Override
public void onPause(){
super.onPause();
whereAmI.disableMyLocation();
}
public void myClickHandler(View target){
switch(target.getId()){
case R.id.sat:
mapView.setSatellite(true);
break;
case R.id.traffic:
mapView.setTraffic(true);
break;
case R.id.normal:
mapView.setSatellite(false);
mapView.setTraffic(false);
break;
}
}
}
[update]
Something more the code for pop up menu is this but i dont want to show the l
#Override
protected boolean onTap(int i) {
OverlayItem item=getItem(i);
GeoPoint geo=item.getPoint();
Point pt=map.getProjection().toPixels(geo, null);
View view=panel.getView();
((TextView)view.findViewById(R.id.latitude))
.setText(String.valueOf(geo.getLatitudeE6()/1000000.0));
((TextView)view.findViewById(R.id.longitude))
.setText(String.valueOf(geo.getLongitudeE6()/1000000.0));
((TextView)view.findViewById(R.id.x))
.setText(String.valueOf(pt.x));
((TextView)view.findViewById(R.id.y))
.setText(String.valueOf(pt.y));
panel.show(pt.y*2>map.getHeight());
return(true);
}
#Override
public int size() {
return(items.size());}}
class PopupPanel {
View popup;
boolean isVisible=false;
PopupPanel(int layout) {
ViewGroup parent=(ViewGroup)map.getParent();
popup=getLayoutInflater().inflate(layout, parent, false);
popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hide();
}
});
}
View getView() {
return(popup);
}
void show(boolean alignTop) {
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
if (alignTop) {
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.setMargins(0, 20, 0, 0);
}
else {
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.setMargins(0, 0, 0, 60);
}
hide();
((ViewGroup)map.getParent()).addView(popup, lp);
isVisible=true;
}
void hide() {
if (isVisible) {
isVisible=false;
((ViewGroup)popup.getParent()).removeView(popup);
}
}
Try adding an OnTouchListener over your Locations
and use Layout Inflator to show a custom layout with the info you want to show.

android thread reset

my code is:
public class EventDialog2 extends Dialog implements OnTouchListener{
TextView textv;
Context con;
Thread t;
int flag=0;
public EventDialog2(Context context, int dialogslideanim) {
super(context, dialogslideanim);
con = context;
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog2);
getWindow().setGravity(Gravity.BOTTOM);
timing();
LinearLayout line = (LinearLayout) findViewById(R.id.linear2);
line.setOnTouchListener(this);
line.removeAllViews();
for (int i = 0; i < 15; i++) {
TextView tv = new TextView(con);
tv.setText("TestProcess " + i);
tv.setTextColor(Color.BLACK);
line.addView(tv);
}
}
private void timing() {
// TODO Auto-generated method stub
if(flag==1){
t.stop();
flag=0;
}
t = new Thread(new Runnable() {
public void run() {
try {
t.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dismiss();
}
});
t.start();
}
public boolean onTouch(View v, MotionEvent event) {
System.out.println("touched");
flag=1;
timing();
return false;
}
}
Here thread is working perfectly as it will sleep after 3 seconds. But i need that the thread should get reset when we touched in the dialog and should sleep after 3 seconds from the touch. Please help me.
Thanks
You'll need to test if it has been 3 seconds since that last touch before you call dismiss(); Here's how I would do it.
public class EventDialog2 extends Dialog implements OnTouchListener{
TextView textv;
Context con;
Thread t;
int flag=0;
Long lastTouchHappenedAt = System.currentTimeMillis();
Handler handle = new Handler();
public EventDialog2(Context context, int dialogslideanim) {
super(context, dialogslideanim);
con = context;
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog2);
getWindow().setGravity(Gravity.BOTTOM);
handle.postDelayed(delayDismis, 3000);
LinearLayout line = (LinearLayout) findViewById(R.id.linear2);
line.setOnTouchListener(this);
line.removeAllViews();
for (int i = 0; i < 15; i++) {
TextView tv = new TextView(con);
tv.setText("TestProcess " + i);
tv.setTextColor(Color.BLACK);
line.addView(tv);
}
}
private Runnable delayDismis = new Runnable() {
public void run() {
if(System.currentTimeMillis() - lastTouchHappenedAt >= 3000){
dismiss();
{
}
};
public boolean onTouch(View v, MotionEvent event) {
System.out.println("touched");
flag=1;
lastTouchHappenedAt = System.currentTimeMillis();
handle.postDelayed(delayDismis, 3000);
return false;
}
}

aDrawable.SetBounds(...) doesn't work

I can't succeed to put a custom centered overlay item on my map. It always appears centered on bottom and middle. Here is my code:
------------My overlay item class-------------
public class EditionThumbOverlayItem extends OverlayItem {
public EditionThumbOverlayItem(GeoPoint aGeoPoint, Resources resources) {
super("", "", aGeoPoint);
Drawable pinThumbDrawable = resources.getDrawable(R.drawable.pin_thumb);
pinThumbDrawable.setBounds(pinThumbDrawable.getIntrinsicWidth() * (-2 / 5),
pinThumbDrawable.getIntrinsicHeight() * (-2 / 5), pinThumbDrawable.getIntrinsicWidth() * 3 / 5,
pinThumbDrawable.getIntrinsicHeight() * 3 / 5);
setMarker(pinThumbDrawable);
}
}
-------------my itemized overlay class-----------------
public class PinThumbOverlay extends ItemizedOverlay<OverlayItem> {
// Only one item can be set on this overlay
private OverlayItem mEditionThumb;
public PinThumbOverlay(Drawable pDefaultMarker, ResourceProxy pResourceProxy) {
super(pDefaultMarker, pResourceProxy);
}
#Override
public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3) {
return false;
}
#Override
protected OverlayItem createItem(int arg0) {
return mEditionThumb;
}
#Override
public int size() {
if (mEditionThumb == null) {
return 0;
} else {
return 1;
}
}
public void addOverlayItem(OverlayItem overlay) {
mEditionThumb = overlay;
populate();
}
public void removeOverlayItem() {
mEditionThumb = null;
populate();
}
}
-----------------itemized overlay creation----------------------
DefaultResourceProxyImpl defaultResouceProxyImpl = new DefaultResourceProxyImpl(getApplicationContext());
Drawable defaultPinEdition = getResources().getDrawable(R.drawable.pin_thumb);
mPinThumbOverlay = new PinThumbOverlay(defaultPinEdition, defaultResouceProxyImpl);
mMapView.getOverlays().clear();
mMapView.getOverlays().add(mPinThumbOverlay);
-------------------Overlay item creation-------------------------
EditionThumbOverlayItem editionThumb = new EditionThumbOverlayItem(new GeoPoint(mCurrentUserLocation),
getResources());
mPinThumbOverlay.addOverlayItem(editionThumb);
As a note: I use osmdroid map, not default google map api.
It seems that osmdroid overwrites any values you set with the drawble.setBounds() method in the onDrawItem method in ItemizedOverlay:
protected void onDrawItem(final Canvas canvas, final Item item, final Point curScreenCoords) {
final int state = (mDrawFocusedItem && (mFocusedItem == item) ? OverlayItem.ITEM_STATE_FOCUSED_MASK
: 0);
final Drawable marker = (item.getMarker(state) == null) ? getDefaultMarker(state) : item
.getMarker(state);
final HotspotPlace hotspot = item.getMarkerHotspot();
boundToHotspot(marker, hotspot);
// draw it
Overlay.drawAt(canvas, marker, curScreenCoords.x, curScreenCoords.y, false);
}
Or rather, the actual overwriting will be done in the boundToHotspot(marker,hotspot); method.
To overcome this set your hotspot on your OverlayItem instead of setting the bounds on the drawable.
Example:
GeoPoint point = new GeoPoint(currentLoc);
OverlayItem item = new OverlayItem("my location", "my loc", point);
item.setMarkerHotspot(HotspotPlace.BOTTOM_CENTER);
This should produce the result your looking for.
If you just want to put a marker in the map center, use this code snippet:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapView.setClickable(true);
mapView.setUseDataConnection(true);
mapView.getController().setZoom(MAP_DEFAULT_ZOOM);
mapView.setTileSource(TileSourceFactory.MAPNIK);
resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
GeoPoint location = getLocation()
ItemizedIconOverlay<OverlayItem> currentLocationOverlay;
Drawable marker = this.getResources().getDrawable(R.drawable.custom_marker);
OverlayItem myLocationOverlayItem = new OverlayItem("Here", "Current Position", location);
myLocationOverlayItem.setMarker(marker);
final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
items.add(myLocationOverlayItem);
currentLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
return true;
}
public boolean onItemLongPress(final int index, final OverlayItem item) {
return true;
}
}, resourceProxy);
mapView.getOverlays().add(this.currentLocationOverlay);
mapView.getController().setCenter(location);
}

How to remove an ItemOverlay on touch and add another on touch?

This is MapOverlay:
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = getResources().getDrawable(R.drawable.splash_logo);
ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable, getApplicationContext());
OverlayItem overlayitem = new OverlayItem(p, "I'm in LA!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mapView.invalidate();
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
String add = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();
i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
//Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
addr = add;
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
else
return false;
}
}
This is ItemizedOverlay:
class ItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem>{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public ItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public ItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void removeOverlay(OverlayItem overlay) {
mOverlays.remove(overlay);
populate();
}
public void clear() {
mOverlays.clear();
populate();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
I'm able to add more than one item overlay with this but not able to call clear so that as to remove the itemziedoverylay item.
What I want to do is this: When I touch on a particular point of the map, the previous itemized overlays should be deleted and a new one at that point should be created. I know I've to call clear() but not able to figure out where exactly should I do it.
Hi you have to write this code in your mapoverlay Class
if (itemizedoverlay != null) {
itemizedoverlay.overlayClear();
}

Categories

Resources