-
Notifications
You must be signed in to change notification settings - Fork 982
/
SampleLimitedScrollArea.java
163 lines (134 loc) · 6 KB
/
SampleLimitedScrollArea.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package org.osmdroid.samplefragments.events;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import org.osmdroid.samplefragments.BaseSampleFragment;
import org.osmdroid.util.BoundingBox;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.Polyline;
import java.util.ArrayList;
/**
* @author Marc Kurtz
*/
public class SampleLimitedScrollArea extends BaseSampleFragment {
// ===========================================================
// Constants
// ===========================================================
public static final String TITLE = "Limited scroll area";
private final int MENU_LIMIT_SCROLLING_LAT_ID = Menu.FIRST;
private final int MENU_LIMIT_SCROLLING_LNG_ID = Menu.FIRST + 1;
private BoundingBox sCentralParkBoundingBox;
public SampleLimitedScrollArea() {
sCentralParkBoundingBox = new BoundingBox(40.796788,
-73.949232, 40.768094, -73.981762);
}
@Override
public String getSampleTitle() {
return TITLE;
}
// ===========================================================
// Constructors
// ===========================================================
//note that since we are not providing the mapview as a constructor parameter,
//the infowindow bubble will not be available
private final Polyline mNorthPolyline = new Polyline();
private final Polyline mSouthPolyline = new Polyline();
private final Polyline mWestPolyline = new Polyline();
private final Polyline mEastPolyline = new Polyline();
@Override
protected void addOverlays() {
super.addOverlays();
final ArrayList<GeoPoint> list = new ArrayList<>();
list.clear();
list.add(new GeoPoint(sCentralParkBoundingBox.getActualNorth(), -85));
list.add(new GeoPoint(sCentralParkBoundingBox.getActualNorth(), -65));
mNorthPolyline.setPoints(list);
mMapView.getOverlays().add(mNorthPolyline);
list.clear();
list.add(new GeoPoint(sCentralParkBoundingBox.getActualSouth(), -85));
list.add(new GeoPoint(sCentralParkBoundingBox.getActualSouth(), -65));
mSouthPolyline.setPoints(list);
mMapView.getOverlays().add(mSouthPolyline);
list.clear();
list.add(new GeoPoint(45, sCentralParkBoundingBox.getLonWest()));
list.add(new GeoPoint(35, sCentralParkBoundingBox.getLonWest()));
mWestPolyline.setPoints(list);
mMapView.getOverlays().add(mWestPolyline);
list.clear();
list.add(new GeoPoint(45, sCentralParkBoundingBox.getLonEast()));
list.add(new GeoPoint(35, sCentralParkBoundingBox.getLonEast()));
mEastPolyline.setPoints(list);
mMapView.getOverlays().add(mEastPolyline);
mMapView.getController().setZoom(13.);
setHasOptionsMenu(true);
mMapView.post(new Runnable() { // "post" because we need View.getWidth() to be set
@Override
public void run() {
setLimitScrollingLatitude(true);
setLimitScrollingLongitude(true);
}
});
}
/**
* @since 6.0.0
*/
private void setLimitScrollingLatitude(boolean pLimitScrolling) {
mMapView.getOverlays().remove(mNorthPolyline);
mMapView.getOverlays().remove(mSouthPolyline);
if (pLimitScrolling) {
mMapView.setScrollableAreaLimitLatitude(sCentralParkBoundingBox.getActualNorth(), sCentralParkBoundingBox.getActualSouth(), mMapView.getHeight() / 2);
mMapView.setExpectedCenter(sCentralParkBoundingBox.getCenterWithDateLine());
mMapView.getOverlays().add(mNorthPolyline);
mMapView.getOverlays().add(mSouthPolyline);
} else {
mMapView.resetScrollableAreaLimitLatitude();
}
mMapView.invalidate();
}
/**
* @since 6.0.0
*/
private void setLimitScrollingLongitude(boolean pLimitScrolling) {
mMapView.getOverlays().remove(mWestPolyline);
mMapView.getOverlays().remove(mEastPolyline);
if (pLimitScrolling) {
mMapView.setScrollableAreaLimitLongitude(sCentralParkBoundingBox.getLonWest(), sCentralParkBoundingBox.getLonEast(), mMapView.getWidth() / 2);
mMapView.setExpectedCenter(sCentralParkBoundingBox.getCenterWithDateLine());
mMapView.getOverlays().add(mWestPolyline);
mMapView.getOverlays().add(mEastPolyline);
} else {
mMapView.resetScrollableAreaLimitLongitude();
}
mMapView.invalidate();
}
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods from SuperClass/Interfaces
// ===========================================================
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add(0, MENU_LIMIT_SCROLLING_LAT_ID, Menu.NONE, "Latitude: Limit scrolling").setCheckable(true);
menu.add(0, MENU_LIMIT_SCROLLING_LNG_ID, Menu.NONE, "Longitude: Limit scrolling").setCheckable(true);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(MENU_LIMIT_SCROLLING_LAT_ID).setChecked(mMapView.isScrollableAreaLimitLatitude());
menu.findItem(MENU_LIMIT_SCROLLING_LNG_ID).setChecked(mMapView.isScrollableAreaLimitLongitude());
super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_LIMIT_SCROLLING_LAT_ID:
setLimitScrollingLatitude(!mMapView.isScrollableAreaLimitLatitude());
return true;
case MENU_LIMIT_SCROLLING_LNG_ID:
setLimitScrollingLongitude(!mMapView.isScrollableAreaLimitLongitude());
return true;
}
return super.onOptionsItemSelected(item);
}
}