我使用的是OpenLayers3,并希望有一个用户可以绘制1个或多个点的地图。 我已经实现了。但是,我也想保存每个点的坐标。
但我真的不知道该怎么做,因为OpenLayers3相当新,我很难在网上找到例子。
这是我到目前为止所做的:
var modeSelect = document.getElementById('type');
var draw; // global so we can remove it later
//modify
var featureOverlay = new ol.FeatureOverlay({
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.2)'
}),
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: '#ffcc33'
})
})
})
});
featureOverlay.setMap(map);
// modify end
function addInteraction() {
var value = modeSelect.value;
if (value == 'Point') {
draw = new ol.interaction.Draw({
//source: source,
features: featureOverlay.getFeatures(),
type: /** @type {ol.geom.GeometryType} */ (value)
});
map.addInteraction(draw);
}
// modify
if (value == 'Modify') {
var modify = new ol.interaction.Modify({
features: featureOverlay.getFeatures(),
// the SHIFT key must be pressed to delete vertices, so
// that new vertices can be drawn at the same position
// of existing vertices
deleteCondition: function(event) {
return ol.events.condition.shiftKeyOnly(event) &&
ol.events.condition.singleClick(event);
}
});
map.addInteraction(modify);
}
}
addInteraction();
答案 0 :(得分:0)
确定。我想出了一个至少朝着正确方向迈出一步的解决方案。 我重新安排了代码并实现了这个功能:
function saveData() {
// save it as geojson
var format = new ol.format['GeoJSON']();
// this will be the data in the chosen format
var data;
try {
// convert the data of the vector_layer into the chosen format
data = format.writeFeatures(vector_layer.getSource().getFeatures());
} catch (e) {
// at time of creation there is an error in the GPX format (18.7.2014)
$('#data').val(e.name + ": " + e.message);
return;
}
$('#data').val(JSON.stringify(data, null, 4));
}
我仍然在努力将它写入服务器上的实际.json文件, 但我非常有信心找到解决方案。
编辑:
实际上非常简单。刚写完这篇完成所有工作的php:
<?php
include 'dbconnection.php';
$str="INSERT INTO multipoint_tabelle (id, geom) VALUES (1,(SELECT ST_Multi(ST_Union(geom)) FROM punkttabelle));";
$test_str=pg_query($conn, $str);
// write GeoJSON
$json = $_POST['json'];
//$points_coords = $_POST['coords'];
if (json_decode($json) != null) { /* sanity check */
// w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
$file = fopen('points.json','w+');
fwrite($file, $json);
fclose($file);
} else {
// handle error
}
?>