将标记保存到csv文件

时间:2017-05-11 01:33:11

标签: java android maps osmdroid

我想知道如何在用户关闭应用时,将标记保存到文件中,然后在再次打开应用时将其重新加载 这是我的代码:

package com.example.mapapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.widget.Button;

import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.OverlayItem;

import java.io.File;
import java.util.ArrayList;


public class MainActivity extends Activity
{

    MapView mv;
    StringBuffer filePath;
    File file;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);

        // This line sets the user agent, a requirement to download OSM maps
        Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
        setContentView(R.layout.activity_main);

        mv = (MapView)findViewById(R.id.map1);
        ItemizedIconOverlay<OverlayItem> items;


        mv.setBuiltInZoomControls(true);
        mv.getController().setZoom(11);
        mv.getController().setCenter(new GeoPoint(53.3710,-1.4502));

    }
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if(item.getItemId() == R.id.addpoi)
        {
            // react to the menu item being selected...
            Intent intent = new Intent(this,AddPoi.class);
            startActivityForResult(intent,0);

            return true;
        }
        Intent intent = new Intent(this,AddPoi.class);
        startActivityForResult(intent,0);

        return false;

    }

    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.menu_mad_assignment, menu);
        return true;
    }
    protected void onActivityResult(int requestCode,int resultCode,Intent intent)
    {

        if(requestCode==0)
        {
            if (resultCode==RESULT_OK)
            {
                Bundle extras=intent.getExtras();
                String poiName= extras.getString("poiName");
                String poiType= extras.getString("poiType");
                String poiDesc= extras.getString("poiDesc");
                ItemizedIconOverlay<OverlayItem> items = new ItemizedIconOverlay<>(this, new ArrayList<OverlayItem>(), null);
                OverlayItem marker = new OverlayItem(poiName, poiType,poiDesc, new GeoPoint(53.3710,-1.4502));
                items.addItem(marker);
                mv.getOverlays().add(items);


                Log.d("NAME",poiName);
                Log.d("NAME",poiType);
                Log.d("NAME",poiDesc);
            }
        }
    }


}

添加标记代码:

package com.example.mapapp;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.example.luke.madassignment.R;

public class AddPoi extends Activity implements OnClickListener {

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_poi);
        Button addPoiButton = (Button)findViewById(R.id.addPoi);
        Button cancelButton = (Button)findViewById(R.id.cancel);
        addPoiButton.setOnClickListener(this);
        cancelButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle=new Bundle();
        EditText poiName = (EditText)findViewById(R.id.editTextPoiName);
        EditText poiType = (EditText)findViewById(R.id.editTextPoiType);
        EditText poiDesc = (EditText)findViewById(R.id.editTextPoiDesc);
        String poiNameString = poiName.toString();
        String poiTypeString = poiName.toString();
        String poiDescString = poiName.toString();
        bundle.putString("poiName",poiNameString);
        bundle.putString("poiType",poiTypeString);
        bundle.putString("poiDesc",poiDescString);
        intent.putExtras(bundle);
        setResult(RESULT_OK,intent);
        finish();

    }
}

问题是我无法找到一种方法将用户标记保存到文件中,如果应用程序关闭,然后在应用程序再次打开时加载回标记

2 个答案:

答案 0 :(得分:0)

使用内置的sharedPref,简单的键值存储,以持久模式保存数据。查看doc here

  

SharedPreferences类提供了允许的通用框架   您可以保存和检索原始数据的持久键值对   类型。您可以使用SharedPreferences保存任何原始数据:   布尔,浮子,整数,长整数和字符串。这些数据将持续存在   跨用户会话(即使你的应用程序被杀死)。

https://developer.android.com/guide/topics/data/data-storage.html#pref

答案 1 :(得分:0)

使用OSMBonusPack,您可以grab your markers in a KML structure,然后在本地保存KML文件(在onStop中)。 并在打开应用程序时(在onCreate中)加载此KML文件。