简体   繁体   English

OpenLayers保存一项功能

[英]OpenLayers save a feature

I'm creating a line which can be edited using this code: 我正在创建一个可以使用以下代码编辑的行:

    var line_points = Array();
    var lineLayer = new OpenLayers.Layer.Vector(label);
    this.map.addLayer(lineLayer);
    this.map.addControl(new OpenLayers.Control.DrawFeature(lineLayer, OpenLayers.Handler.Path));

    for ( x in points ) {
        var point = points[x].split(',');
        var lat = parseFloat( point[0] );
        var lon = parseFloat( point[1] );
        line_points[x] = new OpenLayers.Geometry.Point(this.Lon2Merc(lon), this.Lat2Merc(lat));
    }

    var line = new OpenLayers.Geometry.LineString(line_points);
    var lineFeature = new OpenLayers.Feature.Vector(line, null, style);

    lineLayer.addFeatures([lineFeature]);

I'm trying to pull the latitudes and longitudes out of this feature whenever a point is moved or created to update a HTML form which is then used to save that data. 每当移动或创建一个点来更新HTML表单(然后用于保存该数据)时,我都会尝试将纬度和经度拉出此功能。 I've heard about the protocols and stuff, but don't think that's the right thing for me. 我听说过协议和内容,但不认为这对我来说是正确的。

Could someone point me in the right direction please? 有人能指出我正确的方向吗?

EDIT: I've tried getting the details from the HTML directly: 编辑:我已经尝试直接从HTML获取详细信息:

    var x = 0;
    var y = 0;
    $.each(document.getElementById("OpenLayers.Geometry.LineString_195").getAttribute("points").split(','), function(index, value) {
        if(index%2){
            y = value;
            console.log(MercatorToLatLon(x,y).Lat + "," + MercatorToLatLon(x,y).Lon);
        } else {
            x = value;
        }
        //console.log(value);
    });

but this doesn't seem to give any useful values, and in no particular order which I find strange. 但这似乎没有给出任何有用的价值,而且没有特别的顺序我觉得奇怪。

You can use the getVertices operation to retrieve all the latest latitudes and longitudes of the points in the feature. 您可以使用getVertices操作来检索要素中各点的所有最新纬度和经度。

So call the following whenever you want to update your html. 因此,每当您想要更新html时,请调用以下内容。

console.log(lineFeature.geometry.getVertices());

Update per comment: 每条评论更新:

function report(event) {
    console.log(event.feature.geometry.getVertices());
    console.log(event.type, event.feature ? event.feature.id : event.components);
}
lineLayer.events.on({
    "beforefeaturemodified": report,
    "featuremodified": report,
    "afterfeaturemodified": report,
    "vertexmodified": report,
    "sketchmodified": report,
    "sketchstarted": report,
    "sketchcomplete": report
});

Accessing geometry values directly from dom is probably not the best idea. 直接从dom访问几何值可能不是最好的主意。 Better and easier to use OpenLayers API. 更好,更容易使用OpenLayers API。

You can subscribe to layer's featuremodified event and then access modified feature: 您可以订阅图层的featuremodified事件,然后访问修改后的功能:

lineLayer.events.on({"featuremodified": function(feature){
    console.log(feature.geometry.getVertices());
}});

You can listen to other events too, for instance "featureadded", "featureremoved", etc. 您也可以收听其他活动,例如“featureadded”,“featureremoved”等。

Hope you get the idea. 希望你明白这个主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM