in Education by
I have a polygon feature in OpenLayers 5. It has some random id which I need to show at the center of the polygon slightly right aligned. I have used the ol.style.Text() style to display the id on polygon. I can manage the alignment using the offsetX and offsetY options of the class but how can I display the text in html elements or imitate it, because ol.style.Text() only accepts text data. Overlays in openlayers will definitely solve the problem, I was able get middle point of polygon using getInteriorPoint() on geometry but I don't want to use overlays because there could be large number of polygons on the map and adding overlay for each would deteriorate performance and memory utilization. Here is the expected output / I am trying to achieve : Here is my code : Current code Also check what I have done to toggle the Id ON and OFF and mention if that can be bettered. The ID could be turned ON and OFF based on zoom level. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Instead of using CSS you could draw the background in a canvas element and use it in an icon style. And use a style function to style the interior point of a polygon without needing to create more features: var canvas = document.createElement("canvas"); canvas.width = ??; canvas.height = ??; var ctx = canvas.getContext("2d"); // draw an arraw and rounded box ..... ..... var iconUrl = canvas.toDataURL(); var offStyle = new ol.style.Style({ fill: new ol.style.Fill({ color: 'rgba(255,255,255,0)' }), stroke: new ol.style.Stroke({ color: 'green', width: 1.5 }) }); var onStyle = new ol.style.Style({ fill: new ol.style.Fill({ color: 'rgba(255,255,255,0)' }), stroke: new ol.style.Stroke({ color: 'black', width: 1.5 }) }); var styleFunction = function (feature, resolution) { if (off) { // or use (resolution > ????) return offStyle; else { var styles = [onStyle]; if (feature.getGeometry().getType == 'Polygon') { styles.push( new ol.style.Style({ geometry: feature.getGeometry().getInteriorPoint(), image: new ol.style.Icon({ src: iconUrl, // options to anchor the icon .... }), text: new ol.style.Text({ scale: 1, text: feature.get('.....'), font: 'normal 10px FontAwesome', fill: new ol.style.Fill({ color: 'black' }), }), zIndex: 100 })); } return styles; } }

Related questions

0 votes
    I've extracted certain elements from a HTML page with beautifulsoup and want to extract the according CSS ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 3, 2022 in Education by JackTerrance
0 votes
    As I tried to add a video on my top-wrapper previously. (Thank you so much for those who leave ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I'm using Zend Framework and I have a form input button with a style that I defined. I need to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    I'm using Zend Framework and I have a form input button with a style that I defined. I need to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Nikunj has written the following style rule in the CSS file. H1(font-style=oblique; ) Is anything wrong with the ... yes, correct it. Select the correct answer from above options...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    If you have used inline, internal, and external all the 3 stylings in HTML for a single element then which style will be ... . Inline 2. Internal 3. External 4. None of the above...
asked Jul 12, 2021 in Technology by JackTerrance
0 votes
    We are in the process to update an existing WordPress website and imported posts from one site to another. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    We are in the process to update an existing WordPress website and imported posts from one site to another. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    We are in the process to update an existing WordPress website and imported posts from one site to another. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    We are in the process to update an existing WordPress website and imported posts from one site to another. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    which section is used for text and tags that are shown directly on your web page? A head B body. c title. d HTML Select the correct answer from above options...
asked Dec 27, 2021 in Education by JackTerrance
0 votes
    which section is used for text and tags that are shown directly on your web page? A head B body. c title. d HTML Select the correct answer from above options...
asked Dec 26, 2021 in Education by JackTerrance
0 votes
    I want to right align alternate list elements and keep the other left making it a ZIG-ZAG list Something ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I would like to ask if I require a Mac for the entire process of the iPhone app development or ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    What is the difference between html and CSS?...
asked Dec 21, 2020 in Technology by JackTerrance
...