Openlayers 3 Real-time tracking track-get the last added feature

Source: Internet
Author: User

Problem description

Sometimes, there is a need to query the position of an object at intervals, and then draw the position on the map, the middle point is marked with a normal style, and the current position (that is, the newest point position) uses a special style. We can do this, query to a new location, add pity Dorado to the map, give it a special style, when you query to a new location, get the last added feature, delete its special style, and add a new feature point location.

So, here's the question: How do you get the last added element?

Method One: Getfeatures

First, we know that the ol.source.Vector getFeatures method is provided, which gets all the features in the layer and returns an array of its composition, defined as follows:

Ol.source.Vector.prototype.getFeatures = function() {  varFeaturesif( This. Featurescollection_) {features = This. Featurescollection_.getarray (); }Else if( This. featuresrtree_) {features = This. Featuresrtree_.getall ();if(!goog.object.isempty ( This. Nullgeometryfeatures_) {goog.array.extend (features, Goog.object.getValues ( This. Nullgeometryfeatures_)); }} goog.asserts.assert (features!==undefined,' Neither Featuresrtree_ nor Featurescollection_ are available ');returnfeatures;};

The function first checks if Featurescollection_ is empty, and if it is not empty, then calls its GetArray method, Featurescollection_ is an ol. The Collection object, which is a further encapsulation of the openlayers array of JavaScript, GetArray method is to return an array of objects containing all the features contained in the layer, and if Featurecollection_ is empty , the featuresrtree_ is further checked for NULL, if not empty, gets all the features in it, gets all the features in the Nullgeometryfeatures_, and then merges two arrays, returns, and finally, if there are no features, then an error: " There are no elements in Featuresrtree_ and Featurescollection_ ", if there is a feature, return.

So what is the structure of Featurescollection_ and featuresrtree_? Let us explain each of these.

1.1 Featurescollection_

Featurescollection_ is an object that contains a collection of features, ol.Collection unless the source object is useSpatialIndex set to false , or its features property is set to ol.Collection a feature collection, the value of Featurescollection_ Will be null .

So if it is not the above configuration, then the often getFeatures method is to get the collection of features contained in the Featuresrtree_.

1.2 Featuresrtree_

Featuresrtree_ is defined in Ol.source.Vector as follows:

  /**   * @private   * @type {ol.structs.RBush.<ol.Feature>}   */  thisnewnull;

Class is initialized, the Usespatialindex is first checked (its default value is True), and if true, it is initialized to the ol.structs.RBush object type, otherwise null. So ol.structs.RBush what is this type?

ol.structs.RBushOpenlayers is the re-encapsulation of Rbush, Rbush is a high-performance JavaScript implementation of 2D spatial indexing algorithms for points and rectangles, based on an optimized r-tree data structure that supports bulk insertion. Do not understand the possibility of GIS to "spatial index" is not very understanding, "spatial index" is actually a special data structure, mainly for the data type is a point or a rectangle, "spatial index" allows you to efficiently execute the query criteria into a rectangular area of the query, "spatial query", than the circular retrieval of all items have ' Hundreds of times times ' of Ascension! Spatial indexes are commonly used for map and data visualization.

We can see from the source code above getfeatures that it uses the Ol.structs.Rbush GetAll method, its GetAll method calls the Rbush all () method, but this method of returning all the data does not guarantee that it is in the order of inserting the data , which is easy to understand, let's look at R-tree in memory:


Figure 1 R-tree in memory

As you can see, when inserting a piece of data, this data is added to the rectangular area it contains, but does not record the ordinal number of its insertion, so when all nodes are taken out, it is not guaranteed to follow the order of insertion.

So we can not write the following code, expect to ' always ' get ' the last element added ', the reason is ' always ' because, sometimes can be obtained, but not stable, I test is, when more than 10 will be error:

var features = veclayer.getSource().getFeatures();var feature_num = features.length;var last_feature = features[feature_num-1];
Method Two: Getfeaturebyid

So is there any other way to get the last inserted element?

Ol.source.Vector also provides a method: Getfeaturebyid, we can get it through the ID of the feature, provided that you set the ID of the feature, through the Feature.setid (ID) settings, as mentioned in the opening requirements, In the case of automatic program acquisition, we can set a counter, each time we add a feature to increment the value of the counter by 1, and then set the value of the counter to the ID of the corresponding added feature, when the new point location is obtained, the last added feature is obtained by the counter, and its style is set to Normal style.

Summarize

In this paper, we mainly discuss the method of acquiring the last added elements in real-time trajectory tracking problem, and discussed the reasons why the Ol.source.Vector method gets the order of the elements without rules.

In addition, Rbush's author is Vladimir Agafonkin, whose project location is here, interested to see: Https://github.com/mourner/rbush.

OK, just write here, what's the problem, you can leave a message under the article or send me an e-mail.

Openlayers 3 Real-time tracking track-get the last added feature

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.