Skip to content

Commit b774a9b

Browse files
ecgrebsarahsnow1
authored andcommitted
Place picker map position (#335)
* Forward onDestroy() events from MapView to MapzenMap * Save current map position onDestroy * Save zoom, rotation, and tilt too * Do not re-init map on configuration change * Set default zoom before calculating bounding box * Javadoc
1 parent c201623 commit b774a9b

File tree

7 files changed

+89
-1
lines changed

7 files changed

+89
-1
lines changed

core/src/main/java/com/mapzen/android/graphics/MapView.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class MapView extends RelativeLayout {
4040
TextView attribution;
4141

4242
private int overlayMode = OVERLAY_MODE_SDK;
43+
private MapzenMap mapzenMap;
4344

4445
/**
4546
* Create new instance.
@@ -241,6 +242,11 @@ public TextView getAttribution() {
241242
* You must call this method from the parent Activity/Fragment's corresponding method.
242243
*/
243244
public void onDestroy() {
245+
mapzenMap.onDestroy();
244246
getTangramMapView().onDestroy();
245247
}
248+
249+
void setMapzenMap(MapzenMap mapzenMap) {
250+
this.mapzenMap = mapzenMap;
251+
}
246252
}

core/src/main/java/com/mapzen/android/graphics/MapzenMap.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public boolean onRotate(float x, float y, float rotation) {
136136
this.overlayManager = overlayManager;
137137
this.mapStateManager = mapStateManager;
138138
this.labelPickHandler = labelPickHandler;
139+
mapView.setMapzenMap(this);
139140
mapController.setPanResponder(internalPanResponder);
140141
mapController.setRotateResponder(internalRotateResponder);
141142
overlayManager.restoreMapData();
@@ -832,4 +833,14 @@ private void restoreMapState() {
832833
setTilt(mapStateManager.getTilt());
833834
setCameraType(mapStateManager.getCameraType());
834835
}
836+
837+
/**
838+
* Invoked by {@link MapView} when the parent activity or fragment is destroyed.
839+
*/
840+
void onDestroy() {
841+
mapStateManager.setPosition(mapController.getPosition());
842+
mapStateManager.setZoom(mapController.getZoom());
843+
mapStateManager.setRotation(mapController.getRotation());
844+
mapStateManager.setTilt(mapController.getTilt());
845+
}
835846
}

core/src/test/java/com/mapzen/android/graphics/MapViewTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ public class MapViewTest {
9191
assertThat(layoutInflater.getRoot()).isEqualTo(mapView);
9292
}
9393

94+
@Test public void onDestroy_shouldDestroyMapzenMap() throws Exception {
95+
MapzenMap map = mock(MapzenMap.class);
96+
mapView.setMapzenMap(map);
97+
mapView.onDestroy();
98+
verify(map).onDestroy();
99+
}
100+
94101
private void initMockAttributes(Context context, TypedArray typedArray,
95102
TestLayoutInflater layoutInflater, int overlayMode) {
96103
when(context.obtainStyledAttributes(any(AttributeSet.class), eq(R.styleable.MapView)))

core/src/test/java/com/mapzen/android/graphics/MapzenMapTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class MapzenMapTest {
4545
private TestMapController mapController;
4646
private OverlayManager overlayManager;
4747
private LabelPickHandler labelPickHandler;
48+
private MapStateManager mapStateManager;
4849

4950
@Before public void setUp() throws Exception {
5051
mapView = new TestMapView();
@@ -54,8 +55,16 @@ public class MapzenMapTest {
5455
doCallRealMethod().when(mapController).pickFeature(anyFloat(), anyFloat());
5556
doCallRealMethod().when(mapController).queueSceneUpdate(any(SceneUpdate.class));
5657
doCallRealMethod().when(mapController).getSceneUpdate();
58+
doCallRealMethod().when(mapController).setPosition(any(LngLat.class));
59+
doCallRealMethod().when(mapController).getPosition();
60+
doCallRealMethod().when(mapController).setZoom(anyFloat());
61+
doCallRealMethod().when(mapController).getZoom();
62+
doCallRealMethod().when(mapController).setRotation(anyFloat());
63+
doCallRealMethod().when(mapController).getRotation();
64+
doCallRealMethod().when(mapController).setTilt(anyFloat());
65+
doCallRealMethod().when(mapController).getTilt();
5766
overlayManager = mock(OverlayManager.class);
58-
MapStateManager mapStateManager = new MapStateManager();
67+
mapStateManager = new MapStateManager();
5968
labelPickHandler = new LabelPickHandler(mapView);
6069
map = new MapzenMap(mapView, mapController, overlayManager, mapStateManager, labelPickHandler);
6170
}
@@ -527,6 +536,31 @@ public class MapzenMapTest {
527536
verify(mapController).applySceneUpdates();
528537
}
529538

539+
@Test public void onDestroy_shouldPersistMapPosition() throws Exception {
540+
LngLat position = new LngLat(1, 2);
541+
mapController.setPosition(position);
542+
map.onDestroy();
543+
assertThat(mapStateManager.getPosition()).isEqualTo(position);
544+
}
545+
546+
@Test public void onDestroy_shouldPersistMapZoom() throws Exception {
547+
mapController.setZoom(15);
548+
map.onDestroy();
549+
assertThat(mapStateManager.getZoom()).isEqualTo(15);
550+
}
551+
552+
@Test public void onDestroy_shouldPersistMapRotation() throws Exception {
553+
mapController.setRotation(3.14f);
554+
map.onDestroy();
555+
assertThat(mapStateManager.getRotation()).isEqualTo(3.14f);
556+
}
557+
558+
@Test public void onDestroy_shouldPersistMapTilt() throws Exception {
559+
mapController.setTilt(1.57f);
560+
map.onDestroy();
561+
assertThat(mapStateManager.getTilt()).isEqualTo(1.57f);
562+
}
563+
530564
public class TestRotateResponder implements TouchInput.RotateResponder {
531565

532566
boolean rotated = false;

core/src/test/java/com/mapzen/android/graphics/TestMapController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public TestMapController() {
2828
super(new GLSurfaceView(getMockContext()));
2929
}
3030

31+
@Override public void setPosition(LngLat position) {
32+
longitude = position.longitude;
33+
latitude = position.latitude;
34+
}
35+
3136
@Override public void setPositionEased(LngLat lngLat, int duration) {
3237
longitude = lngLat.longitude;
3338
latitude = lngLat.latitude;

mapzen-places-api/src/main/java/com/mapzen/places/api/internal/PlacePickerActivity.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class PlacePickerActivity extends Activity implements
3939
AlertDialog dialog;
4040
String dialogPlaceId;
4141

42+
private boolean mapInitComplete = false;
43+
4244
@Override protected void onCreate(Bundle savedInstanceState) {
4345
super.onCreate(savedInstanceState);
4446
setContentView(R.layout.place_picker_activity);
@@ -53,6 +55,10 @@ public class PlacePickerActivity extends Activity implements
5355
callbackHandler));
5456
presenter.setController(this);
5557

58+
if (savedInstanceState != null) {
59+
mapInitComplete = true;
60+
}
61+
5662
mapView = (MapView) findViewById(R.id.mz_map_view);
5763
mapView.getMapAsync(this);
5864
}
@@ -67,6 +73,15 @@ public class PlacePickerActivity extends Activity implements
6773
presenter.onShowView();
6874
}
6975

76+
@Override protected void onDestroy() {
77+
super.onDestroy();
78+
mapView.onDestroy();
79+
}
80+
81+
@Override protected void onSaveInstanceState(Bundle outState) {
82+
super.onSaveInstanceState(new Bundle());
83+
}
84+
7085
@Override public void setMyLocationEnabled(boolean enabled) {
7186
if (map != null) {
7287
map.setMyLocationEnabled(enabled);
@@ -130,6 +145,15 @@ public class PlacePickerActivity extends Activity implements
130145
private void initializeMap() {
131146
map.setMyLocationEnabled(true);
132147
map.setLabelPickListener(this);
148+
map.setPersistMapState(true);
149+
if (!mapInitComplete) {
150+
initMapView();
151+
}
152+
}
153+
154+
private void initMapView() {
155+
// The calculation below fails if map.getZoom() returns 0 so set a reasonable default zoom.
156+
map.setZoom(15);
133157

134158
Bundle extras = getIntent().getExtras();
135159
if (extras != null) {

samples/mapzen-android-sdk-sample/src/main/java/com/mapzen/android/sdk/sample/BasicMapzenActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private void configureMap(boolean enabled) {
7777

7878
map.setCompassButtonEnabled(true);
7979
map.setZoomButtonsEnabled(true);
80+
map.setPersistMapState(true);
8081
}
8182

8283
@Override protected void onPause() {

0 commit comments

Comments
 (0)