2016-10-01 19 views
1

私はOLの新人です。私はちょうどマップ上のいくつかのポイントを置くために理想的には、このポイントについての情報を書き込むためにいくつかの要求を行う必要がありますが、今私はちょうどポイント(警備)を警告したい。だから、私はいくつかのポイント、ライン、ポリゴンをマップに入れることができる例を見つけました。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に保存するための権利を行う方法?

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

}

+0

'event.feature.getGeometry()。getCoordinates()'の代わりに 'feature.getGeometry()。getCoordinates()'を試してみてください。 – ahocevar

+0

このポイントを地図にロードする方法を知っていますか、新しい質問をする必要がありますか? =) –

答えて

0

ちょうどこのコードを墨消しするのに必要なI

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コン​​トローラに私はちょうどからわずかタイトルとCOORDSを取得し、アクションを書かれていますリクエスト。それは安全ではありませんが、私はこれを修正します。 地図上に置かれたすべての点、線、またはポリは、座標をPostgresSQLのDataBaseに保存します。

関連する問題