나는 OL의 뉴비입니다. 난 그냥지도에 몇 가지 지점을두고 이상적으로 db에이 지점에 대한 정보를 writting에 대한 몇 가지 요청을해야하지만, 지금은 (지점)의 경고 (coords) 싶어요. 그래서 예제를 발견했습니다. 여기서 점, 선 및 다각형을지도에 넣을 수 있습니다. Openlayers 3 Coords
var raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
var map = new ol.Map({
layers: [raster],
target: 'map',
view: new ol.View({
center: [-11000000, 4600000],
zoom: 4
})
});
var features = new ol.Collection();
var featureOverlay = new ol.layer.Vector({
source: new ol.source.Vector({features: features}),
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);
var modify = new ol.interaction.Modify({
features: features,
// 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) {
//var feature = event.element;
//var coord = event.feature.getGeometry().getCoordinates();
//coord = ol.proj.transform(coord, 'EPSG:3857', 'EPSG:4326');
// alert(coord);
return ol.events.condition.shiftKeyOnly(event) &&
ol.events.condition.singleClick(event);
}
});
map.addInteraction(modify);
var draw; // global so we can remove it later
var typeSelect = document.getElementById('type');
function addInteraction() {
draw = new ol.interaction.Draw({
features: features,
type: /** @type {ol.geom.GeometryType} */ (typeSelect.value)
/*draw.on('drawend', function (event) {
// get the feature
var feature = event.element;
var coord = event.feature.getGeometry().getCoordinates();
alert(coord);*/
});
map.addInteraction(draw);
}
/**
* Handle change event.
*/
typeSelect.onchange = function() {
map.removeInteraction(draw);
addInteraction();
};
addInteraction();
// Code of adding to DB our features
//
<?php
/* @var $this yii\web\View */
use yii\helpers\Html;
use sibilino\yii2\openlayers\ol;
use sibilino\yii2\openlayers\OpenLayers;
?>
<!DOCTYPE html>
<html>
<head>
<title>Draw and Modify Features</title>
<link rel="stylesheet" href="http://openlayers.org/en/v3.18.2/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="http://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="http://openlayers.org/en/v3.18.2/build/ol.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<form class="form-inline">
<label>Geometry type </label>
<select id="type">
<option value="Point">Point</option>
<option value="LineString">LineString</option>
<option value="Polygon">Polygon</option>
</select>
</form>
<script>
</script>
</body>
</html>
그것은 모든 libs가 정상적으로 작동하고, 내가지도에 점, 선 및 polygones를 넣을 수 있습니다,하지만 난 넣어 포인트의 COORD을 얻을 수
. 나는 그것이이 코드에 설명으로, 일부 청취자를 만들려고//var feature = event.element;
//var coord = event.feature.getGeometry().getCoordinates();
//coord = ol.proj.transform(coord, 'EPSG:3857', 'EPSG:4326');
// alert(coord);
처럼 보인다하지만 이해되는이 방법은 마우스가 너무 이동, 그리고 내가 좋아하는 몇 가지 오류를 받고 있어요 수신 :
Uncaught TypeError: Cannot read property 'getGeometry' of undefined
하는 경우 누군가가이 긴 질문을 읽을 시간이 있고, 무엇을 할 수 있으며, 내 코디를 받고 DB에 저장하는 방법을 제대로 수행 할 수 있습니까?
'event.feature.getGeometry(). getCoordinates()'대신'feature.getGeometry(). getCoordinates()'를 시도하십시오. – ahocevar
이 포인트를지도에로드하는 방법을 알고 계시나요? 아니면 새로운 질문을해야합니까? =) –