[android] Save added markers in test app

Fixes #2877
This commit is contained in:
Leith Bade
2015-10-30 15:44:17 +11:00
parent e0fd0fc071
commit 009930ab8e
2 changed files with 43 additions and 16 deletions

View File

@@ -55,10 +55,14 @@ public class MainActivity extends AppCompatActivity {
// Used for saving instance state
private static final String STATE_IS_ANNOTATIONS_ON = "isAnnotationsOn";
private static final String STATE_SELECTED_STYLE = "selectedStyle";
private static final String STATE_MARKER_LIST = "markerList";
// Used for permissions requests
private static final int PERMISSIONS_LOCATION = 0;
// Used for info window
private static final DecimalFormat LAT_LON_FORMATTER = new DecimalFormat("#.#####");
//
// Instance members
//
@@ -76,8 +80,7 @@ public class MainActivity extends AppCompatActivity {
// Used for Annotations
private boolean mIsAnnotationsOn = false;
private static final DecimalFormat latLngFormatter = new DecimalFormat("#.#####");
private ArrayList<MarkerOptions> mMarkerList = new ArrayList<>();
//
// Lifecycle events
@@ -119,20 +122,23 @@ public class MainActivity extends AppCompatActivity {
mMapView.setOnMapLongClickListener(new MapView.OnMapLongClickListener() {
@Override
public void onMapLongClick(@NonNull LatLng point) {
mMapView.addMarker(new MarkerOptions()
MarkerOptions marker = new MarkerOptions()
.position(point)
.title("Dropped Pin")
.snippet(latLngFormatter.format(point.getLatitude()) + ", " +
latLngFormatter.format(point.getLongitude()))
.icon(null));
.snippet(LAT_LON_FORMATTER.format(point.getLatitude()) + ", " +
LAT_LON_FORMATTER.format(point.getLongitude()))
.icon(null);
mMarkerList.add(marker);
mMapView.addMarker(marker);
}
});
mMapView.setOnMapClickListener(new MapView.OnMapClickListener() {
@Override
public void onMapClick(@NonNull LatLng point) {
String location = latLngFormatter.format(point.getLatitude()) + ", " +
latLngFormatter.format(point.getLongitude());
String location = LAT_LON_FORMATTER.format(point.getLatitude()) + ", " +
LAT_LON_FORMATTER.format(point.getLongitude());
Snackbar.make(mCoordinatorLayout, "Map Click Listener " + location, Snackbar.LENGTH_SHORT).show();
}
});
@@ -190,6 +196,8 @@ public class MainActivity extends AppCompatActivity {
if (savedInstanceState != null) {
mIsAnnotationsOn = savedInstanceState.getBoolean(STATE_IS_ANNOTATIONS_ON);
mSelectedStyle = savedInstanceState.getInt(STATE_SELECTED_STYLE);
mMarkerList = savedInstanceState.getParcelableArrayList(STATE_MARKER_LIST);
mMapView.addMarkers(mMarkerList);
}
// Set default UI state
@@ -249,6 +257,7 @@ public class MainActivity extends AppCompatActivity {
mMapView.onSaveInstanceState(outState);
outState.putBoolean(STATE_IS_ANNOTATIONS_ON, mIsAnnotationsOn);
outState.putInt(STATE_SELECTED_STYLE, mSelectedStyle);
outState.putParcelableArrayList(STATE_MARKER_LIST, mMarkerList);
}
// Called when the system is running low on memory
@@ -515,6 +524,7 @@ public class MainActivity extends AppCompatActivity {
}
private void removeAnnotations() {
mMarkerList.clear();
mMapView.removeAllAnnotations();
}

View File

@@ -6,25 +6,26 @@ import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.GestureDetector;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.utils.ApiAccess;
import com.mapbox.mapboxsdk.views.MapView;
import java.text.DecimalFormat;
import java.util.ArrayList;
public class PressForMarkerActivity extends AppCompatActivity implements MapView.OnMapLongClickListener {
private MapView mMapView;
private ArrayList<MarkerOptions> mMarkerList = new ArrayList<>();
private static final DecimalFormat latLonFormatter = new DecimalFormat("#.#####");
private static final DecimalFormat LAT_LON_FORMATTER = new DecimalFormat("#.#####");
private static String STATE_MARKER_LIST = "markerList";
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -47,19 +48,35 @@ public class PressForMarkerActivity extends AppCompatActivity implements MapView
mMapView.setZoomLevel(11);
mMapView.setOnMapLongClickListener(this);
((ViewGroup) findViewById(R.id.activity_container)).addView(mMapView);
if (savedInstanceState != null) {
mMarkerList = savedInstanceState.getParcelableArrayList(STATE_MARKER_LIST);
mMapView.addMarkers(mMarkerList);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
outState.putParcelableArrayList(STATE_MARKER_LIST, mMarkerList);
}
@Override
public void onMapLongClick(@NonNull LatLng point) {
final PointF pixel = mMapView.toScreenLocation(point);
String title = latLonFormatter.format(point.getLatitude()) + ", " + latLonFormatter.format(point.getLongitude());
String title = LAT_LON_FORMATTER.format(point.getLatitude()) + ", " + LAT_LON_FORMATTER.format(point.getLongitude());
String snippet = "X = " + (int) pixel.x + ", Y = " + (int) pixel.y;
mMapView.addMarker(new MarkerOptions()
MarkerOptions marker = new MarkerOptions()
.position(point)
.title(title)
.snippet(snippet));
.snippet(snippet);
mMarkerList.add(marker);
mMapView.addMarker(marker);
}
/**