如何保存和恢复标记数组
Posted
技术标签:
【中文标题】如何保存和恢复标记数组【英文标题】:How to save and restore marker array 【发布时间】:2020-12-05 23:11:52 【问题描述】:我需要保存触发的位置,以便在我再次打开应用程序时显示它们。
我使用 int array 来保存 onMarkerClick 方法中触发位置的数量。并使用此数组在 createMarkers 方法中创建所需的标记。 我试图保存和恢复这个数组,但它不起作用。
Activity.class 的部分内容
public class MapsActivity extends FragmentActivity
implements
OnRequestPermissionsResultCallback,
OnMapReadyCallback,
OnMarkerClickListener
private LatLng[] latLngArray;
private String[] titleArray;
private Marker[] markerArray;
private int[] resIdArray;
private int ARRAY_SIZE = 7;
private int COUNTER = 0;
private static final float DISTANCE = 50;
private boolean START_POINT_CHECKED = false;
public final String inArrayName= "inArray";
//public int outArray[] = new int[ARRAY_SIZE];
public int inArray[];
private Location mCurrentLocation;
private GoogleMap mMap;
private MediaPlayer mediaPlayer;
@Override
protected void onSaveInstanceState(@NonNull Bundle outState)
outState.putIntArray(outArrayName,inArray);
super.onSaveInstanceState(outState);
@Override
protected void onRestoreInstanceState(@NonNull Bundle inState)
inArray = inState.getIntArray(outArrayName);
super.onRestoreInstanceState(inState);
@Override
protected void onCreate(Bundle inState)
inArray = new int[ARRAY_SIZE];
super.onCreate(inState);
setContentView(R.layout.activity_maps);
if(inState == null)
for (int i=0; i<ARRAY_SIZE;i++)
inArray[i]=0;
private void createMarkersList()
markerArray = new Marker[ARRAY_SIZE];
if(inArray[0] == 777)
markerArray[0] = mMap.addMarker(new MarkerOptions()
.position(latLngArray[0])
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker1))
.title(titleArray[0])
.snippet("0")
.visible(true)
);
markerArray[0].setTag(666);
inArray[0] = 777;
START_POINT_CHECKED = true;
else
markerArray[0] = mMap.addMarker(new MarkerOptions()
.position(latLngArray[0])
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker2))
.title(titleArray[0])
.snippet("0")
.visible(true)
);
markerArray[0].setTag(0);
if (START_POINT_CHECKED)
for (int i = 1; i < ARRAY_SIZE; i++)
if (inArray[i] == 777)
markerArray[i] = mMap.addMarker(new MarkerOptions()
.position(latLngArray[i])
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker1))
.title(titleArray[i])
.snippet(Integer.toString(i))
.visible(true)
);
markerArray[i].setTag(666);
inArray[i]=777;
else
markerArray[i] = mMap.addMarker(new MarkerOptions()
.position(latLngArray[i])
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker2))
.title(titleArray[i])
.snippet(Integer.toString(i))
.visible(true)
);
markerArray[i].setTag(i);
else
for (int i = 1; i < ARRAY_SIZE; i++)
markerArray[i] = mMap.addMarker(new MarkerOptions()
.position(latLngArray[i])
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker2))
.title(titleArray[i])
.snippet(Integer.toString(i))
.visible(false)
);
markerArray[i].setTag(i);
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
mMap.setOnMarkerClickListener(this);
EnableUserLocation();
MapStyling();
createLatLngsList();
createTitlesList();
createMarkersList();
createResIdArray();
@Override
public boolean onMarkerClick(Marker marker)
int id = (int) marker.getTag();
Log.d(TAG, "Marker" + id + "Clicked");
if (getDistance(marker.getPosition(), new LatLng(mCurrentLocation.getLatitude(),mCurrentLocation.getLongitude())) <= DISTANCE)
if ( id == 666)
if (mediaPlayer.isPlaying())
mediaPlayer.pause();
return true;
else
mediaPlayer.start();
else
if(START_POINT_CHECKED)
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.marker1));
if (mediaPlayer!= null)
mediaPlayer.reset();
mediaPlayer = MediaPlayer.create(this, resIdArray[id]);
mediaPlayer.start();
inArray[id] = 777;
marker.setTag(666);
COUNTER++;
if (COUNTER == 7)
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
@Override
public void onCompletion(MediaPlayer mediaPlayer)
showPicture();
);
return true;
else
if(id == 0)
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.marker1));
if (mediaPlayer!= null)
mediaPlayer.reset();
mediaPlayer = MediaPlayer.create(this, resIdArray[id]);
mediaPlayer.start();
inArray[id] = 777;
marker.setTag(666);
COUNTER++;
START_POINT_CHECKED = true;
showMarkers();
return true;
else
return true;
return false;
【问题讨论】:
latLngArray
在哪里设置过任何东西?
【参考方案1】:
如果你的标签对于相同的标记总是相同的,你可以将一个数组放入共享首选项中并在 onMapReady 中恢复这些位置
您可以在 onMarkerClick 方法中添加标签值
【讨论】:
或者您可以使用 Room 例如创建数据库。以上是关于如何保存和恢复标记数组的主要内容,如果未能解决你的问题,请参考以下文章
如何在加载了 JSON API 数据的 tableview 单元格中保存复选标记属性?