Improved method for obtaining ol.Feature.getGeometry().j

When I use ol.Feature.getGeometry().j to retrieve an array of all coordinates [x, y, x, y, x, y...], it works well for points and polygons. This method allows me to move features quickly and smoothly. However, using .j is not the recommended way to access this array. What is the best alternative method?

I prefer not to use getCoordinates() as it returns a different instance of arrays for points and polygons. Additionally, I am wary of using .j because it may change in later versions, requiring me to make adjustments.

To achieve this functionality, I am using ol.js 3.5.0 and jQuery in the example code below:


var x = 5, y = -10;
var l = feature.getGeometry().j, n = [], b = true;
$.each(l, function(i, v) {
    if (b) {
        n.push(v + x);
        b = false;
    } else {
        n.push(v + y);
        b = true;
    }
});
feature.getGeometry().j = n;

Answer №1

In the process of manipulating geometry, the applyTransform() function comes in handy for adjusting the internal representation.

var adjust = function(input, output, dimension) {
  for (var i = 0, ii = input.length; i < ii; i += dimension) {
    output[i] = input[i] + x;
    output[i + 1] = input[i + 1] + y;
  }
};
geometry.applyTransform(adjust);

For more information, refer to ol.TransformFunction

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Which language is better for refreshing web pages: PHP or Javascript?

May I seek your opinion on this matter? I have the ability to refresh my page using two different methods, but I am uncertain of any potential drawbacks. Can you advise me on which one I should utilize? Edit: Specifically, I am referring to the use of he ...

Creating stylish error labels using Materialize CSS

While Materialize has built-in support for validating input fields like email, I am looking to implement real-time validation for password inputs as well. This would involve dynamically adding error or success labels using JavaScript. Unfortunately, my at ...

Creating packing features specifically designed for resolution within a reusable module

I've decided to revamp my Angular application based on John Papa's style guide (well, mostly) and my main focus is on improving modularity. The stumbling block I've encountered is with route resolves. So far, I've been using a global ap ...

Ways to extract specific data from a Json response

Currently, I am engaged in a school project that involves manipulating json data from the Google Geocoding API. I am facing a dilemma on how to properly store the "formatted_address" (as shown below) from the API response so that I can utilize this inform ...

What is the best way to narrow down the content cards displayed on the page?

I have recently developed a blog post featuring three distinct categories: digital marketing, tips and advice, and cryptocurrency. My goal is to implement a filtering system for these categories. For instance, I would like users to be able to click on a b ...

Please explain the purpose of the .forEach statements listed below

Describe the functionality of the two forEach loops provided in the code snippet below. Also, is 'col' a predefined property for arrays? var width = data.length, height = data[0].length; data.forEach(function(col){ col.forEach(function(v ...

Creating a tooltip for a specific cell within an AG grid react component is a useful customization feature

How can I customize the tooltip for a specific row in my AG Grid? I see that there is a tooltipField available in ColDefs, but I want to provide a custom string instead of using a field value. Is there a way to achieve this customization? ...

What is the best method for transferring state between a page and a component, and then back to the page

Is there a way to manage state without using Redux in my home.js page (using hooks, not classes), so that I can set/use a state, pass it to my component MyComponent.js, and update the state when a div is clicked inside this component (reflecting the change ...

Firebase onCall function is executing properly, however, it is only delivering {data: null} as the

I am facing an issue with my cloud function where everything runs smoothly until I attempt to return a response back to the front-end. Despite successfully modifying databases, the final log points D and G still show {data:null} as the output. I have a fe ...

What might be causing req.body to be an empty object?

I'm currently diving into the world of XMLHttpRequests. My goal is to send some input data to the server, but when it reaches its destination, the Object appears empty as if it were {}. I've noticed that by commenting out the setRequestHeader sec ...

Changing scope is ineffective unless $scope.$apply is used

When a button is clicked, I have a directive that displays a loading screen. angular.module('randomButton', ['Data']) .directive('randomButton', function(Data) { return { scope: '=', restrict: ...

What is the functionality of require(../) in node.js?

When encountering var foo=require(../), what specific modules does node.js search for? It may appear to look in the directory above the current one, but what exactly is it seeking and how does it function? Is there a comparison with include in C or impor ...

ng-select search functionality failing to display any matches

Currently, I am encountering an issue with the ng-select functionality in my Angular CLI version 11.2.8 project. Despite using ng-select version 7.3 and ensuring compatibility with the Angular CLI version, the search functionality within the select eleme ...

Output repeated JSON Array in console using Node.js

Looking to identify duplicates within a JSON array based on the URL. var temp = [ { "name":"Allen", "site":"www.google.com/allen" }, { "name":"Chris", "site":&q ...

Removing the Yellow Highlight on Input Field Following Email Autocomplete in Chrome

My username-password form is styled and working perfectly, but there's an issue that arises when I log in multiple times. Chrome automatically fills in my email, turning the username textbox yellow. It doesn't seem to happen with Firefox or Safar ...

No results found when attempting to intersect mouse click raycast on point cloud in Three JS (empty array returned)

I've been working on rendering a point cloud and attempting to select a single point, but unfortunately, the method raycaster.intersectObjects(scene.children, true) is returning an empty array. I've tried various methods to calculate the pointer. ...

Erase all records using MongoDB by the following criteria or

I am currently working with a calendar document structured as shown below: calendar: [ {days: { 1: [], 2: ['surprise'], 3: [], ... }}, {days: { 1: [], 2: [], 3: ['test'], ... }} ] My task involves locating specific ...

Location of Custom HTML Widget in Django-Dashing

I've encountered a dilemma while using the Django-Dashing framework, specifically regarding the placement of my HTML file for a custom widget. I have meticulously configured the code in my dashboard.html file to ensure proper loading. {% extends &apo ...

Adjust the fixed navbar position in MaterializeCSS as you scroll

First of all, I apologize for my limited proficiency in English. I have a website with a company logo at the top and a navigation bar below it. My goal is to change the position of the navigation bar to the top when scrolling past the company logo. I att ...

Scoped variable in Typescript producing a generated Javascript file

I'm currently learning TypeScript through an online course, and I've encountered a problem that seems to be related to a VSCode setting. Whenever I compile app.ts, it generates the app.js file, but I immediately encounter a TypeScript error. It& ...