1 | package org.mapsforge.android.maps;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Collection;
|
---|
5 |
|
---|
6 | import org.mapsforge.android.maps.overlay.ItemizedOverlay;
|
---|
7 | import org.mapsforge.android.maps.overlay.OverlayItem;
|
---|
8 |
|
---|
9 | import android.app.AlertDialog;
|
---|
10 | import android.content.Context;
|
---|
11 | import android.graphics.drawable.Drawable;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Thread-safe implementation of the {@link ItemizedOverlay} class using an {@link ArrayList} as
|
---|
15 | * internal data structure.
|
---|
16 | */
|
---|
17 | public class ArrayItemizedOverlayTweaked extends ItemizedOverlay<OverlayItem> {
|
---|
18 | private static final int ARRAY_LIST_INITIAL_CAPACITY = 8;
|
---|
19 | private static final String THREAD_NAME = "ArrayItemizedOverlayTweaked";
|
---|
20 |
|
---|
21 | protected final Context context;
|
---|
22 | protected AlertDialog.Builder dialog;
|
---|
23 | protected OverlayItem item;
|
---|
24 | protected final ArrayList<OverlayItem> overlayItems;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new ArrayItemizedOverlay.
|
---|
28 | *
|
---|
29 | * @param defaultMarker
|
---|
30 | * the default marker.
|
---|
31 | * @param context
|
---|
32 | * the reference to the application context.
|
---|
33 | */
|
---|
34 | public ArrayItemizedOverlayTweaked(Drawable defaultMarker, Context context) {
|
---|
35 | super(boundCenterBottom(defaultMarker));
|
---|
36 | this.context = context;
|
---|
37 | this.overlayItems = new ArrayList<OverlayItem>(ARRAY_LIST_INITIAL_CAPACITY);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Adds the given item to the Overlay.
|
---|
42 | *
|
---|
43 | * @param overlayItem
|
---|
44 | * the item that should be added to the Overlay.
|
---|
45 | */
|
---|
46 | public synchronized void addOverlay(OverlayItem overlayItem) {
|
---|
47 | this.overlayItems.add(overlayItem);
|
---|
48 | populate();
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Adds all items of the given collection to the Overlay.
|
---|
53 | *
|
---|
54 | * @param c
|
---|
55 | * collection whose items should be added to the Overlay.
|
---|
56 | */
|
---|
57 | public synchronized void addOverlays(Collection<? extends OverlayItem> c) {
|
---|
58 | this.overlayItems.addAll(c);
|
---|
59 | populate();
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Removes all items from the Overlay.
|
---|
64 | */
|
---|
65 | public synchronized void clear() {
|
---|
66 | this.overlayItems.clear();
|
---|
67 | populate();
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public String getThreadName() {
|
---|
72 | return THREAD_NAME;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Removes the given item from the Overlay.
|
---|
77 | *
|
---|
78 | * @param overlayItem
|
---|
79 | * the item that should be removed from the Overlay.
|
---|
80 | */
|
---|
81 | public synchronized void removeOverlay(OverlayItem overlayItem) {
|
---|
82 | this.overlayItems.remove(overlayItem);
|
---|
83 | populate();
|
---|
84 | super.requestRedraw();
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public synchronized int size() {
|
---|
89 | return this.overlayItems.size();
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public synchronized OverlayItem createItem(int i) {
|
---|
94 | return this.overlayItems.get(i);
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | protected synchronized boolean onTap(int index) {
|
---|
99 | this.item = this.overlayItems.get(index);
|
---|
100 | this.dialog = new AlertDialog.Builder(this.context);
|
---|
101 | this.dialog.setTitle(this.item.getTitle());
|
---|
102 | this.dialog.setMessage(this.item.getSnippet());
|
---|
103 | this.dialog.show();
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 |
|
---|
107 | }
|
---|