Google Maps API会保存标记并在下次启动时加载它们

时间:2013-08-09 11:25:36

标签: android google-maps-api-2 markers

相当蹩脚的头衔,但这就是我想要弄清楚的。我希望能够在长时间点击地图上创建标记,然后将这些标记保存在数据库,首选项,数组列表中,我不知道,只是在某些持久性存储中,它们不会被擦除下一次开始申请。我想保持简单,如果只有共享偏好可以,那么请告诉我如何。 这是代码

public class MainActivity extends Activity implements ParsingCallback {

    private GoogleMap mMap;
    Marker marker = null;
    String s;
    GoogleMapOptions options;
    LocationManager lm;
    LatLng userLocation;
    Location location;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        options = new GoogleMapOptions();
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        listOfPoints = new ArrayList<SavedMarkers>();
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setCompassEnabled(true);
        mMap.getUiSettings().setRotateGesturesEnabled(true);
        mMap.getUiSettings().setZoomGesturesEnabled(true);
        mMap.getUiSettings().setMyLocationButtonEnabled(true);
        }

        /** On Map Long Click */
        mMap.setOnMapLongClickListener(new OnMapLongClickListener() {

            @Override
            public void onMapLongClick(LatLng arg0) {
                mMap.addMarker(new MarkerOptions().position(arg0).title(arg0.toString()));
                Toast.makeText(getApplicationContext(), "Marker Created", Toast.LENGTH_SHORT).show();

            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

使用Gson序列化和反序列化SavedMarkers

的数组列表
Type listOfMarkers = new TypeToken<List<SavedMarkers>>(){}.getType();
String stringToSaveInSharedPreferences = gson.toJson(list, listOfMarkers);
List<SavedMarker> markers = gson.fromJson(stringRetrievedFromSharedPreferences,listOfMarkers)