I am utilizing two maps, one is where someone could add or remove features (admin file) and another that the end-users would see (user file). However, I have no way to save the features I added in my admin file upon refreshing in my browser.
I wanted to know if there's a way to save features and pull them up again (for both files)?
//the main map of the app
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.fromLonLat([-90.055, 35.147]),
zoom: 17,
})
});
//---------------------------------------------------------
//---------------------------------------------------------
///this creates the markers for the map
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([-90.055, 35.147])),
name: 'Somewhere else'
});
iconStyle = new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'icon.png',
})
});
//Sets specific style for that one point
iconFeature.setStyle(new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'icon.png',
})
}));
eventFeature.setStyle(new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'icon.png',
})
}));
var eventLayerSource = new ol.source.Vector({
features: [iconFeature]
});
//individual layers for the user to switch through based on selection
var event_Layer = new ol.layer.Vector({
source: eventLayerSource,
// style for all elements on a layer
style: new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'icon.png'
})
}),
visible: true,
tab_name: 'event'
});
// display popup on click
map.on('click', function (evt) {
var coordinate = evt.coordinate;
console.log(evt.coordinate);
console.log(ol.proj.transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326'));
});
map.on('singleclick', function (evt) {
var lonLat = ol.proj.transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326');
var clickFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat(lonLat)),
name: window.prompt("Enter name of event","Event name"),
});
clickFeature.setStyle(iconStyle);
clickFeature.setId(clickFeature.get('name'));
eventLayerSource.addFeature(clickFeature);
});
map.on('dblclick', function(evt){
eventLayerSource.removeFeature(
eventLayerSource.getFeatureById(prompt("Enter name")));
})
This is my code for the admin file. I appreciate any insight as I am new to Openlayers.
You can save features you have drawn by writing them in a format such as GeoJSON or KML and then downloading them to the computer as a text file (just add buttons to your HTML to save/download)
function download(data, filename) {
var blob = new Blob([data], {type: 'text/plain'});
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, filename);
} else {
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
}
}
document.getElementById('download-geojson').addEventListener('click', function () {
var text = new ol.format.GeoJSON().writeFeatures(
source.getFeatures(),
{
featureProjection: 'EPSG:3857',
dataProjection: 'EPSG:4326'
}
);
download(text, 'features.json');
});
document.getElementById('download-kml').addEventListener('click', function () {
var text = new ol.format.KML().writeFeatures(
source.getFeatures(),
{
featureProjection: 'EPSG:3857',
dataProjection: 'EPSG:4326'
}
);
download(text, 'features.kml');
});
The data in the files can be displayed again using Drag and Drop as in https://openlayers.org/en/latest/examples/drag-and-drop.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.