Javascript 在OL3中保存多个点的坐标
我正在使用OpenLayers3,希望有一个地图,用户可以在其中绘制1个或多个点。 我已经实现了。但是,我还想保存每个点的坐标 但我真的不知道如何做到这一点,因为OpenLayers3是相当新的,我很难在网上找到示例 这就是我到目前为止所做的:Javascript 在OL3中保存多个点的坐标,javascript,openlayers-3,geojson,Javascript,Openlayers 3,Geojson,我正在使用OpenLayers3,希望有一个地图,用户可以在其中绘制1个或多个点。 我已经实现了。但是,我还想保存每个点的坐标 但我真的不知道如何做到这一点,因为OpenLayers3是相当新的,我很难在网上找到示例 这就是我到目前为止所做的: var modeSelect = document.getElementById('type'); var draw; // global so we can remove it later //modify var featureOverlay =
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();
嗯。我提出了一个解决方案,至少是朝着正确的方向迈出了一步。 我大体上重新排列了代码并实现了此功能:
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
}
?>
-1对于仅显示代码片段,希望查看完整的JSFIDLE代码或类似的内容。例如,在javascript代码中:什么是向量层?是什么触发了保存功能、按钮或映射功能事件?搜索了您的代码段,找到了以下完整的工作代码: