简体   繁体   English

Openlayers - LayerRedraw() / 特征旋转 / 线串坐标

[英]Openlayers - LayerRedraw() / Feature rotation / Linestring coords

TLDR : I have an Openlayers map with a layer called 'track' I want to remove track and add track back in. Or figure out how to plot a triangle based off one set of coords & a heading(see below). TLDR :我有一个 Openlayers 地图,其中有一个名为“轨道”的图层,我想删除轨道并重新添加轨道。或者弄清楚如何根据一组坐标和标题绘制三角形(见下文)。


I have an image 'imageFeature' on a layer that rotates on load to the direction being set.我在一个图层上有一个图像“imageFeature” ,它在加载时旋转到设置的方向。 I want it to update this rotation that is set in 'styleMap' on a layer called 'tracking' .我希望它更新在名为'tracking'的图层上的'styleMap' 中设置的旋转。

  1. I set the var 'stylemap' to apply the external image & rotation.我设置了 var 'stylemap'来应用外部图像和旋转。
  2. The 'imageFeature' is added to the layer at the coords specified. 'imageFeature'被添加到指定坐标的图层中。
  3. 'imageFeature' is removed. 'imageFeature'被删除。
  4. 'imageFeature' is added again in its new location. 'imageFeature'再次添加到其新位置。 Rotation is not applied..不适用旋转..

As the 'styleMap' applies to the layer I think that I have to remove the layer and add it again rather than just the 'imageFeature'由于“styleMap”适用于图层,我认为我必须删除图层并再次添加它而不仅仅是“imageFeature”

Layer :

var tracking = new OpenLayers.Layer.GML("Tracking", "coordinates.json", {
  format: OpenLayers.Format.GeoJSON,
  styleMap: styleMap
});

styleMap :样式映射

var styleMap = new OpenLayers.StyleMap({
  fillOpacity: 1,
  pointRadius: 10,
  rotation: heading,
});

Now wrapped in a timed function the imageFeature :现在包含在一个定时函数 imageFeature 中

map.layers[3].addFeatures(new OpenLayers.Feature.Vector(
  new OpenLayers.Geometry.Point(longitude, latitude), {
    rotation: heading,
    type: parseInt(Math.random() * 3)
  }
));

Type refers to a lookup of 1 of 3 images.类型是指查找 3 张图像中的 1 张。 :

styleMap.addUniqueValueRules("default", "type", lookup);

var lookup = {
  0: {
    externalGraphic: "Image1.png",
    rotation: heading
  },
  1: {
    externalGraphic: "Image2.png",
    rotation: heading
  },
  2: {
    externalGraphic: "Image3.png",
    rotation: heading
  }
}

I have tried the 'redraw()' function: but it returns "tracking is undefined" or "map.layers[2]" is undefined.我尝试过“redraw()”函数:但它返回“跟踪未定义”或“map.layers[2]”未定义。

tracking.redraw(true);
map.layers[2].redraw(true);

Heading is a variable: from a JSON feed.标题是一个变量:来自 JSON 提要。

var heading = 13.542;

But so far can't get anything to work it will only rotate the image onload.但到目前为止还不能做任何事情,它只会在加载时旋转图像。 The image will move in coordinates as it should though.图像将按其应有的坐标移动。

So what am I doing wrong with the redraw function or how can I get this image to rotate live?那么我在重绘功能上做错了什么,或者如何让这个图像实时旋转?

Thanks in advance提前致谢

-Ozaki -尾崎

Add: I managed to get添加:我设法得到

 map.layers[2].redraw(true);

to sucessfully redraw layer 2. But it still does not update the rotation.成功重绘第2层。但它仍然没有更新旋转。 I am thinking because the stylemap is updating.我在想是因为样式图正在更新。 But it runs through the style map every n sec, but no updates to rotation and the variable for heading is updating correctly if i put a watch on it in firebug.但是它每 n 秒运行一次样式映射,但是如果我在萤火虫中放置手表,则不会更新旋转,并且标题的变量正在正确更新。


If I were to draw a triangle with an array of points & linestring.如果我要绘制一个带有点和线串数组的三角形。 How would I go about facing the triangle towards the heading.我将如何面对三角形朝向标题。 I have the Lon/lat of one point and the heading.我有一点的经纬度和航向。

var points = new Array(
  new OpenLayers.Geometry.Point(lon1, lat1),
  new OpenLayers.Geometry.Point(lon2, lat2),
  new OpenLayers.Geometry.Point(lon3, lat3)
);

var line = new OpenLayers.Geometry.LineString(points);

Looking for any way to solve this problem Image or Line anyone know how to do either added a 100rep bounty I am really stuck with this.寻找任何方法来解决这个问题 Image 或 Line 任何人都知道该怎么做,要么添加了100rep 的赏金,我真的很坚持这个。


//From getJSON request//
var heading = data.RawHeading;

Adding and removing the imageFeature添加和删​​除 imageFeature

Solved the problem as follows:问题解决如下:

            var styleMap = new OpenLayers.StyleMap({
                fillOpacity: 1,
                pointRadius: 10,
                rotation: "${angle}",
            });

           var lookup = {
                0: { externalGraphic: "Image1.png", rotation: "${angle}" },
                1: { externalGraphic: "Image2.png", rotation: "${angle}" },
                2: { externalGraphic: "Image3.png", rotation: "${angle}" }
            }
 styleMap.addUniqueValueRules("default", "type", lookup);

 map.layers[3].addFeatures(new OpenLayers.Feature.Vector(
        new OpenLayers.Geometry.Point(lon, lat), {"angle": dir, type: parseInt(Math.random() * 3)}
        ), {"angle": dir});

then the request:然后请求:

var dir = (function () {
                $.ajax({
                    'async': false,
                    'global': true,
                    'url': urldefault,
                    'dataType': "json",
                    'success': function (data) {
                        dir = data.Heading
                    }
                });
                return dir;
            })();

Problem solved.问题解决了。 Works perfectly.完美运行。

You can also try to put heading on the object as an attribute:您还可以尝试将标题作为属性放在对象上:

{"mapFeatures": {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "id": "1579001",
                "x": 51.0,
                "y": 1.2,
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        51.0,
                        1.2
                    ],
                    "crs": {
                        "type": "OGC",
                        "properties": {
                            "urn": "urn:ogc:def:crs:OGC:1.3:CRS84"
                        }
                    }
                },
                "properties": {
                    "heading": 45,
                    "label": "some_label_goes_here"
                }
            }
        ]
    }
}

Then you would have to rewrite your lookup function like this:然后你必须像这样重写你的查找函数:

var lookup = {
      0: {externalGraphic: "Image1.png", rotation: ${heading}},
      1: {externalGraphic: "Image2.png", rotation: ${heading}},
      2: {externalGraphic: "Image3.png", rotation: ${heading}}
}

Could you try that and see if it works?你能试试这个,看看它是否有效? If you don' t know if the attributes are set correctly, you can always debug with firebug, that is what I always do.如果你不知道属性设置是否正确,你可以随时使用firebug进行调试,我一直都是这样做的。 There is one tricky thing;有一件棘手的事情; when parsing geojson;解析geojson时; "properties" are translated to "attributes" on the final javascript object.在最终的 javascript 对象上,“属性”被翻译成“属性”。

First guess:第一个猜测:

I assume your layer has a single point object that moves and rotates as when following a car with GPS?我假设您的图层有一个单点对象,可以像跟随带有 GPS 的汽车一样移动和旋转?

It might be better if you would simply destroy all features on the layer (assuming it is only one feature) and redraw the feature with the new heading set.如果您简单地销毁图层上的所有要素(假设它只有一个要素)并使用新的标题集重绘该要素,则可能会更好。

Second guess: Perhaps you need to use a function instead of a variable to maintain the live connection to the rotation.第二个猜测:也许您需要使用函数而不是变量来维护与旋转的实时连接。

Please check the documentation here: http://trac.openlayers.org/wiki/Styles on styles.请在此处查看文档: http : //trac.openlayers.org/wiki/Styles关于样式。

Hope this helps a bit.希望这个对你有帮助。

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

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