在我的应用程序中,我有一张地图,因此当用户点击地图时,他们可以添加自定义标记。现在的问题是,当我添加了一些标记并离开应用程序时,所有标记都消失了。我需要保存这些标记,以便当应用程序再次启动或用户离开地图并返回时,它们的标记应该仍然存在。
这是我的活动:
public class Test extends Activity implements OnMapLongClickListener, OnMapClickListener, OnMarkerClickListener{
private GoogleMap googleMap;
static final LatLng Point1 = new LatLng(xx.xxx, xx.xxx);
static final LatLng Point2 = new LatLng(xx.xxx, xx.xxx);
static final LatLng Point3 = new LatLng(xx.xxx, xx.xxx);
static final LatLng Point4 = new LatLng(xx.xxx, xx.xxx);
Bitmap bitmap;
private static final int TAKE_PICTURE = 0;
private static Uri fileUri;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_googlemaps);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
try {
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
public void onResume() {
super.onResume();
initilizeMap();
if (googleMap!=null){
googleMap.addMarker(new MarkerOptions().position(Point1)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
googleMap.addMarker(new MarkerOptions().position(Point2)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
googleMap.addMarker(new MarkerOptions().position(Point3)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
googleMap.addMarker(new MarkerOptions().position(Point4)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
}
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(xx.xxx, xx.xxx)).zoom(9).bearing(0).tilt(80).build();
googleMap.setOnMapClickListener(this);
googleMap.setOnMapLongClickListener(this);
googleMap.setOnMarkerClickListener(this);
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(false);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}
public void onMapLongClick(final LatLng point) {
Intent getCameraImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getApplicationContext().getDir(
getResources().getString(R.string.app_name), MODE_PRIVATE);
fileUri = Uri.fromFile(new File((Environment.getExternalStorageDirectory() +
"/" +getResources().getString(R.string.app_name)),new Date().getTime() + ".jpg"));
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(getCameraImage, TAKE_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
try {
GetImageThumbnail getImageThumbnail = new GetImageThumbnail();
bitmap = getImageThumbnail.getThumbnail(fileUri, this);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
{
MarkerOptions markerOptions = new MarkerOptions()
.position(Position)
.icon(BitmapDescriptorFactory
.fromBitmap(bitmap));
googleMap.addMarker(markerOptions);
}
}
}
public void onMapClick (LatLng point){
}
@Override
public boolean onMarkerClick(final Marker marker) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context)
.setIcon(R.drawable.ic_dialog);
// set title
alertDialogBuilder.setTitle("My Marker");
// set dialog message
alertDialogBuilder
.setMessage("Select Option")
.setCancelable(false)
.setPositiveButton("Display full Image",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imgUri = Uri.parse("file://" + fileUri);
intent.setDataAndType(imgUri, "image/*");
startActivity(intent);
}
})
.setNegativeButton("Delete Marker",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
marker.remove();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
return false;
}
}
希望有人可以提供帮助 感谢
答案 0 :(得分:0)
Google地图不会为您保留标记。您应该保存数据库中的位置并在启动时重新加载信息(在onCreate()
中,并以异步方式更好)。
OT:您使用的是已弃用的API,getMap()
已替换为getMapAsync()