简体   繁体   English

在OpenLayers中创建复杂的特征/矢量

[英]Creating a complex Feature / Vector in OpenLayers

The closest post I could find to my question is Compound complex feature in OpenLayers . 我能找到的最接近我的问题的帖子是OpenLayers中的Composite复合功能 Alas, no one answered it. las,没有人回答。 I am quite proficient in JavaScript but relatively new to OpenLayers and its complex API. 我精通JavaScript,但对OpenLayers及其复杂的API还是比较陌生。 I have created complex Controls prior to this. 在此之前,我已经创建了复杂的控件。 However, this time I am looking to create a complex Feature / Vector. 但是,这次我要创建一个复杂的特征/矢量。 The general idea of it is that the feature has a display icon (like a pin, for example) as the main component. 其基本思想是该功能部件具有一个显示图标(例如,类似图钉)作为主要组件。 The component is interactive and responds to user actions (select, drag, etc). 该组件是交互式的,可响应用户操作(选择,拖动等)。 Upon selection, I desire to render additional vectors that are logically associated with this component (circles, rectangles, etc). 选择后,我希望渲染逻辑上与此组件相关的其他向量(圆形,矩形等)。 These Vectors listen to user interactions as well. 这些向量也侦听用户交互。

Previously, in case of Controller, I was able to use source of other controllers to make sense of development direction and successfully proceed. 以前,在使用Controller的情况下,我能够使用其他控制器的源来了解开发方向并成功进行。 Its a little harder with Features / Vectors, imho. 恕我直言,“功能/向量”有点难。

I started by extending OpenLayers.Feature.Vector using OpenLayers.Feature.Vector.CustomClass = OpenLayers.Class( OpenLayers.Feature.Vector, {...}); 我首先使用OpenLayers.Feature.Vector.CustomClass = OpenLayers.Class( OpenLayers.Feature.Vector, {...});扩展OpenLayers.Feature.Vector OpenLayers.Feature.Vector.CustomClass = OpenLayers.Class( OpenLayers.Feature.Vector, {...}); code. 码。 Constructor takes specific parameters to my feature, creates several geometry objects (points, polygon, lines), adds them to the OpenLayers.Geometry.Collection, and invokes OpenLayers.Feature.Vector constructor with collection passed into it. 构造函数将特定参数用于我的特征,创建几个几何对象(点,多边形,线),将其添加到OpenLayers.Geometry.Collection,然后调用OpenLayers.Feature.Vector构造函数并将其传递给集合。

Unfortunately, I realized that in order to display an icon, I cannot just use a Geometry.Point but need to create a Vector for it. 不幸的是,我意识到为了显示图标,我不仅可以使用Geometry.Point,还需要为其创建一个Vector。 That kind of threw me off because I will create Vectors within my custom Vector object. 这种情况使我失望,因为我将在自定义Vector对象中创建Vector。 It is nothing unusual in general but I wonder if this is the way things are done in OpenLayers. 一般而言,这没什么不寻常的,但是我想知道这是否是OpenLayers中完成工作的方式。 Like I have mentioned, I do not find API documentation very useful as it simply states general function headers / brief description. 就像我已经提到的那样,我发现API文档不是非常有用,因为它只声明了常规函数头/简短描述。

I would greatly appreciate if someone could point me into the right direction (haven't found many tutorials online beyond the basic "create marker with custom image" types). 如果有人能指出我正确的方向,我将不胜感激(除了基本的“使用自定义图像创建标记”类型之外,还没有在线找到许多教程)。 If the description isn't clear, let me know and I'll try to provide additional information. 如果描述不清楚,请告诉我,我将尝试提供其他信息。

I've had to tackle similar problems in the past. 过去,我不得不解决类似的问题。 The best approach with OpenLayers (or any mapping tool for that matter) is usually to separate your layers into feature classes, each of which represents a collection of points, lines, or polygons. 使用OpenLayers(或与此相关的任何映射工具)的最佳方法通常是将图层分为要素类,每个要素类代表点,线或面的集合。 Once you create all of your layers, you can create a select control that listens for events on each of these layers and responds appropriately. 一旦创建了所有层,就可以创建一个select控件,以侦听每个层上的事件并做出适当的响应。

If you need to logically associate subsets of these features together, you could store references to these features externally, or within the parent feature's attributes object. 如果需要将这些功能的子集在逻辑上关联在一起,则可以在外部或在父功能的attributes对象中存储对这些功能的引用。

My solution is to provide a FeatureCollection geojson as the complex/compound type data. 我的解决方案是提供FeatureCollection geojson作为复杂/复合类型数据。 In my case the FeatureCollection consists of many Point features, and one LineString feature. 就我而言,FeatureCollection由许多Point特征和一个LineString特征组成。 Openlayers can consume this geojson: Openlayers可以使用以下geojson:

var features = (new ol.format.GeoJSON()).readFeatures(geojson)

... and provide the collection of features. ...并提供功能集合。 You can then iterate over those features and provide some unifying attribute/object to each feature. 然后,您可以遍历这些功能,并为每个功能提供一些统一的属性/对象。 Then, when you define event handlers (hover or select/click), access the unifying attribute to get hold of any other related feature. 然后,当您定义事件处理程序(悬停或选择/单击)时,访问统一属性以获取任何其他相关功能。

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

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