Java 在android地图上添加五个标记

Java 在android地图上添加五个标记,java,android,google-maps,google-maps-markers,Java,Android,Google Maps,Google Maps Markers,我正在开发一个应用程序,我正在使用mapv2,在这个应用程序中,我需要将五个位置保存到SharedReferences中,因为我使用了arraylist,这很好,稍后我想在调用oncreate()方法时获取所有位置 但我的问题是,假设我先保存2个位置,然后单击“上一步”按钮,然后再次转到我的地图,它在地图上显示了我以前保存的2个位置,现在我保存了另外3个位置,然后单击“上一步”按钮,然后再次转到我的地图,现在它只显示最后3个位置,但我想要总共5个位置。 在这里,我发现了一个问题,即,第二个3位置

我正在开发一个应用程序,我正在使用mapv2,在这个应用程序中,我需要将五个位置保存到SharedReferences中,因为我使用了arraylist,这很好,稍后我想在调用oncreate()方法时获取所有位置

但我的问题是,假设我先保存2个位置,然后单击“上一步”按钮,然后再次转到我的地图,它在地图上显示了我以前保存的2个位置,现在我保存了另外3个位置,然后单击“上一步”按钮,然后再次转到我的地图,现在它只显示最后3个位置,但我想要总共5个位置。

在这里,我发现了一个问题,即,第二个3位置数组被前2个位置数组覆盖,我试图解决这个问题,但我没有得到。或者,这个问题的任何原因请告诉我。 我将删除我的代码,接受任何类型的帮助。 提前谢谢

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_or_delete_favourate_loc);
    addressListView =(ListView)findViewById(R.id.listView);
    clearButton = (Button)findViewById(R.id.clearBtn);
    _googlemap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
            R.id.favarouteLocMap)).getMap(); 
    _googlemap.getUiSettings().setZoomControlsEnabled(true);
    _googlemap.getUiSettings().setCompassEnabled(true);
    _googlemap.getUiSettings().setMyLocationButtonEnabled(true);
    hydLocation = new LatLng(17.3752800, 78.4744400);
    _googlemap.moveCamera(CameraUpdateFactory.newLatLngZoom(hydLocation, 10));
    _googlemap.setOnMapClickListener(this);
     sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    editor = sharedPreferences.edit();

   addressList = new ArrayList<String>(5);
    distanceList = new ArrayList<String>();
    latLngList = new ArrayList<LatLng>(5);
    markerList = new ArrayList<Marker>();
    latArray = new ArrayList<Float>();
    lngArray = new ArrayList<Float>();
    ArrayList<String> addAList=new ArrayList<String>();
    int size1=sharedPreferences.getInt("size1",0);
    int size2=sharedPreferences.getInt("size2",0);
    int size3=sharedPreferences.getInt("size3",0);

    for(int j=0;j<size1;j++)
    {
        addAList.add(sharedPreferences.getString("addr"+j,""));
    }


     adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,addAList);
     addressListView.setAdapter(adapter);
     ArrayList<Float> latList = new ArrayList<Float>();
     for(int j=0;j<size2;j++){
         latList.add(sharedPreferences.getFloat("latArr"+j,0.0f));
     }
     Log.e("LAT", ""+latList.size());
     ArrayList<Float> lngList = new ArrayList<Float>();
     for(int j=0;j<size3;j++){
         lngList.add(sharedPreferences.getFloat("lngArr"+j,0.0f));
     }
     Log.e("LNG", ""+lngList.size());
     for(int i=0;i<latList.size();i++){
          lat1=latList.get(i);
          lng1=lngList.get(i);
          Log.e("LAT LNG", ""+lat1+lng1);
         latLng = new LatLng((double)lat1,(double)lng1);
         getMarks(_googlemap, latLng);
     }
}

@Override
public void onMapClick(final LatLng point) {
    // TODO Auto-generated method stub
     lat = point.latitude;
     lng = point.longitude;
     count=sharedPreferences.getInt("count",0);
     count++;

     editor.clear();
    Geocoder geoCoder = new Geocoder(AddOrDeleteFavourateLocActivity.this, Locale.getDefault());
    try{
         List<Address> addresses = geoCoder.getFromLocation(point.latitude, point.longitude, 1);
         if (addresses != null) {
             Address returnedAddress = addresses.get(0);
             StringBuilder strReturnedAddress = new StringBuilder("");

             for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                 strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
             }
             final String strAdd = strReturnedAddress.toString();

             alertDialogBuilder = new AlertDialog.Builder(
                    AddOrDeleteFavourateLocActivity.this);
            alertDialogBuilder.setTitle("Enter Distance From Your Location");
            input = new EditText(AddOrDeleteFavourateLocActivity.this);
            input.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
            alertDialogBuilder.setView(input);
            alertDialogBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    distance=input.getText().toString();
                     diskm ="\n"+distance+" km";
                     if(count==1){

                         addressList.add(strAdd+diskm);
                         latLngList.add(point);
                         markerList.add(marker1);
                         latArray.add((float)lat);
                         lngArray.add((float)lng);
                         editor.putInt("count", count);

                     }
                     if(count==2){

                         addressList.add(strAdd+diskm);
                         latLngList.add(point);
                         markerList.add(marker2);
                         latArray.add((float)lat);
                         lngArray.add((float)lng);

                         editor.putInt("count", count);
                     }
                     if(count==3){

                         addressList.add(strAdd+diskm);
                         latLngList.add(point);
                         markerList.add(marker3);
                         latArray.add((float)lat);
                         lngArray.add((float)lng);
                         editor.putInt("count", count);

                     }
                     if(count==4){

                         addressList.add(strAdd+diskm);
                         latLngList.add(point);
                         markerList.add(marker4);
                         latArray.add((float)lat);
                         lngArray.add((float)lng);
                         editor.putInt("count", count);

                     }
                     if(count==5){

                         addressList.add(strAdd+diskm);
                         latLngList.add(point);
                         markerList.add(marker5);
                         latArray.add((float)lat);
                         lngArray.add((float)lng);
                         editor.putInt("count", count);
                     }
                   for(int i=0;i<addressList.size();i++)
                     {
                       editor.putString("addr"+i,addressList.get(i));
                     }
                     for(int i=0;i<latArray.size();i++)
                     {
                       editor.putFloat("latArr"+i,latArray.get(i));
                     }
                     for(int i=0;i<lngArray.size();i++)
                     {
                       editor.putFloat("lngArr"+i,lngArray.get(i));
                     }
                     editor.putInt("size1",addressList.size());
                     editor.putInt("size2",latArray.size());
                     editor.putInt("size3",lngArray.size());

                    editor.commit();


                }
            });
            alertDialogBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            });
             if(count==1){
                 marker1= getMarks(_googlemap, point);
                alertDialogBuilder.show();

             }
             if(count==2){
                  marker2=getMarks(_googlemap, point);
                  alertDialogBuilder.show();

             }
             if(count==3){
                  marker3=getMarks(_googlemap, point);
                  alertDialogBuilder.show();

             }
             if(count==4){
                  marker4=getMarks(_googlemap, point);
                  alertDialogBuilder.show();

             }
             if(count==5){
                 marker5=getMarks(_googlemap, point);
                 alertDialogBuilder.show();

             }
             if(count>5){
                 Toast.makeText(getApplicationContext(),"Sorry You Can Add Only Five Locations", 100).show();
             }


             ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,addressList);
             addressListView.setAdapter(adapter);
         }
    }
    catch(Exception e){
        e.printStackTrace();
    }

}
private Marker getMarks(GoogleMap googleMap,LatLng arg0){
    Marker marker;
     MarkerOptions markerOption = new MarkerOptions();
    markerOption.position(arg0).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)).flat(true);
    markerOption.draggable(true);

     marker=googleMap.addMarker(markerOption);
     return marker;
}
public void clearData(View v){
    _googlemap.clear();
    editor.clear().commit();
    adapter.clear();
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u add\u或\u delete\u Favorite\u loc);
addressListView=(ListView)findViewById(R.id.ListView);
clearButton=(按钮)findViewById(R.id.clearBtn);
_googlemap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(
R.id.favarouteLocMap().getMap();
_googlemap.getUiSettings().setZoomControlsEnabled(true);
_googlemap.getUiSettings().setCompassEnabled(true);
_googlemap.getUiSettings().setMyLocationButtonEnabled(true);
hydLocation=新车床(17.3752800,78.4744400);
_googlemap.moveCamera(CameraUpdateFactory.newLatLngZoom(hydLocation,10));
_setOnMapClickListener(this);
SharedReferences=PreferenceManager.GetDefaultSharedReferences(getApplicationContext());
editor=SharedReferences.edit();
地址列表=新的阵列列表(5);
距离列表=新的ArrayList();
latLngList=新阵列列表(5);
markerList=newarraylist();
latArray=新的ArrayList();
lngArray=新的ArrayList();
ArrayList addList=新的ArrayList();
int size1=SharedReferences.getInt(“size1”,0);
int size2=SharedReferences.getInt(“size2”,0);
int size3=SharedReferences.getInt(“size3”,0);
对于(int j=0;j
ArrayList addAList=null;
@凌驾
SaveInstanceState上的公共无效(Bundle savedInstanceState){
savedInstanceState.putParcelableArrayList(“地址”,addAList);
super.onSaveInstanceState(savedInstanceState);
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_布局);
如果(savedInstanceState!=null){
addAList=(ArrayList)savedInstanceState.getSerializable(“地址”);
}
否则{
Toast msg=Toast.makeText(getApplicationContext(),“值未还原!!!”,Toast.LENGTH\u LONG);
msg.setGravity(Gravity.CENTER,msg.getXOffset()/2,msg.getYOffset()/2);
msg.show();
}
}

在onCreate方法的顶部创建arraylist变量..我仅在顶部创建了arraylist AddList=new arraylist();它是在onCreate()方法内部创建的..更改了,但结果相同将变量设置为静态
ArrayList<String> addAList= null;
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
   savedInstanceState.putParcelableArrayList("address", addAList);
  super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.your_layout);

    if(savedInstanceState!=null){
    addAList = (ArrayList<String>) savedInstanceState.getSerializable("address");
    }
    else{
        Toast msg = Toast.makeText(getApplicationContext(), "Values Not Restored!!!", Toast.LENGTH_LONG);
        msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
        msg.show();
        }
}