2016-10-01 5 views
1

나는 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 &nbsp;</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에 저장하는 방법을 제대로 수행 할 수 있습니까?

+0

'event.feature.getGeometry(). getCoordinates()'대신'feature.getGeometry(). getCoordinates()'를 시도하십시오. – ahocevar

+0

이 포인트를지도에로드하는 방법을 알고 계시나요? 아니면 새로운 질문을해야합니까? =) –

답변

0

난 그냥이 코드를 편집하다하는 데 필요한 :

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); 

}

사람 :

function addInteraction() { 

draw = new ol.interaction.Draw({ 
    features: features, 
    type: /** @type {ol.geom.GeometryType} */ (typeSelect.value) 

}); 

// Code of adding to DB our features 

draw.on('drawend', function (event) { 
    var feature = event.element; 
    var coord = event.feature.getGeometry().getCoordinates(); 
alert(coord); 

    var title=document.getElementById('type'); 

    var url = "http://localhost/basic/web/index.php?r=sggis/create&title="+title.value+"&point="+coord; 
    function lol(){ 
    var xhr = new XMLHttpRequest(); 
    xhr.open("GET", url, false); 
    xhr.send(); 
    } 
    lol(); 
}); 

그리고 yii2 컨트롤러에 난 그냥 단지에서 제목과 좌표를 취득하는 조치를 작성했습니다를 요청. 그것은 안전하지 못하지만, 나는 이것을 수정합니다. 지도에 올린 모든 점, 선 또는 폴리은 좌표를 PostgresSQL의 DataBase에 저장합니다.