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