0

I have created some code to create two Polygons in Openlayers Map and tried to keep the size same while zooming it, but the size is also reducing when I zoomout of the map. My intention is to use Polygons as site markers. Could you please help me to use Polygons as site markers and to keep the size same while zoom out and zoom in?

I have used parcel bundler to create the test build. I have seen the OverLay example in your site where the marker stays at the same size while doing zoom In/Out of the Map ,similarly I am looking for possibility to make use of polygons as site markers.

For simplicity I have removed the import part of the below JS code.

Below is the JS Code:

//Position of our map center

var pos = fromLonLat([76.87403794962249, 8.569385045000772]);

//Position for our Triangle Polygon

var pos1 = fromLonLat([76.85860825505787, 8.575525035547585]);

//The below line is to check the Longitude and Latitude
//console.log(toLonLat(pos1));

var pos2 = fromLonLat([76.85286067404068, 8.56925661298456]);

var pos3 = fromLonLat([76.86300346314657, 8.56917303421666]);

//Position for arrow Polygon

var arrowOne = fromLonLat([76.86219331461274, 8.565926475435887]);

var arrowTwo = fromLonLat([76.86584111887299, 8.566053785302557]);

var arrowThree = fromLonLat([76.86566945749604, 8.56758150037902]);

var arrowFour = fromLonLat([76.87034723001801, 8.56456850087342]);

var arrowFive = fromLonLat([76.86635610300385, 8.562064722566959]);

var arrowSix = fromLonLat([76.86627027231538, 8.5638470749155]);

var arrowSeven = fromLonLat([76.86163541513764, 8.564016822322785]);

//OSM() Tile layer for our Map

var tileLayer = new TileLayer({
    source: new OSM()
});

//Setting View for our Map

var viewOne = new View({
    center: pos,
    zoom: 15
});

//Coordinates for our Polygons
var cordTriangle = [pos1, pos2, pos3, pos1];
var cordArrow = [arrowOne, arrowTwo, arrowThree, arrowFour, arrowFive, 
arrowSix, arrowSeven, arrowOne];

var polyTriangle = new Polygon([cordTriangle]);

var polyArrow = new Polygon([cordArrow])

//Adding the Feature for our Polygons
var featureTriangle = new Feature(polyTriangle);

var featureArrow = new Feature(polyArrow);

//vectorSource.addFeature(feature);

var vectorSource = new VectorSource({
    projection: 'EPSG:4326',
    features: [featureTriangle, featureArrow]
});

// The below Select is needed if we have to select a feature before move
//var select = new Select();

//The below will select all the Features and add it for Translate
var translate = new Translate();

//Setting custom styles for our Polygons

featureTriangle.setStyle(new Style({
    fill: new Fill({
        color: 'red'
    })
}));

featureArrow.setStyle(new Style({
    stroke: new Stroke({
        color: 'green',
        width: 2
    }),
    fill: new Fill({
        color: 'cyan'
    })
}));

var vectorLayer = new VectorLayer({
    source: vectorSource,
    updateWhileAnimating: true,
    updateWhileInteracting: true
});



// Adding all Layers and creating our Map
var map = new Map({
    interactions: defaultInteractions().extend([ /*select,*/ translate]),
    target: 'map',
    layers: [tileLayer, vectorLayer],
    view: viewOne
});


//To get the Lon and Lat of clicked location over map. This will be 
displayed in the console.

$(document).ready(function () {
    map.on('click', function (event) {
        let cordClick = toLonLat(event.coordinate);
        console.log(cordClick);
    });
});

Changes added on 31/08/2018:

var resol = viewOne.getResolution();

console.log('The Resolution is : ' + resol);

var cordVary = function () {

    for (var outer = 0; outer < cordArrow.length; outer++) {

        var cordArrow1 = [
            [0, 0],
            [0, 0],
            [0, 0],
            [0, 0],
            [0, 0],
            [0, 0],
            [0, 0],
            [0, 0]
        ];

        for (var inner = 0; inner < cordArrow[outer].length; inner++) {

            cordArrow1[outer][inner][0] = cordArrow[outer][inner][0] * resol;
            cordarrow1[outer][inner][1] = cordArrow[outer][inner][1] * resol;
            //console.log(outer, inner);
        }
    }
    return cordArrow1;
}

I am trying to update the coordinates when I zoom into the map to keep the same size for the Polygons

CodeBuggy
  • 429
  • 5
  • 18
  • 1
    You better create the polygon as image and add it as marker. polygons(features) intend to stand with its coordinates so should be updating it size up to zoom. look at your code to implement static size polygon. it's kind of mess and not very correct. – Chase Choi Aug 24 '18 at 01:29
  • okay @ChaseChoi So Could you please share the steps to put a polygon as an image and adding it to an ovelay? – CodeBuggy Aug 24 '18 at 06:11
  • recommend you to use other tools like paints or photoshop to create polygon image. – Chase Choi Aug 24 '18 at 06:23
  • Thanks @ChaseChoi When I searched the Marker in Openlayers API, there is nothing related to Markers. I think Markers are deprecated.So Could you share some code snippet to add the image Polygon and creating as an overlay? – CodeBuggy Aug 24 '18 at 06:33
  • see `overlay` example http://openlayers.org/en/latest/examples/overlay.html – Chase Choi Aug 24 '18 at 06:38
  • Thanks @ChaseChoi I will have a look and let you know – CodeBuggy Aug 24 '18 at 09:16
  • @ChaseChoi I checked that sample.But for an Ovelay, we can only add it to an HTML element right? How can I add a polygon image to that ovelay? – CodeBuggy Aug 24 '18 at 10:22
  • I mean, you need polygon as image! not the polygon something as feature. use photoshop to create the image. – Chase Choi Aug 24 '18 at 12:27
  • Yes @ChaseChoi I am not sure how to put a normal image in an overlay. The sample overlay which you showed above only have a CSS marker. – CodeBuggy Aug 24 '18 at 12:31
  • You may need to use Icon style. http://openlayers.org/en/latest/apidoc/module-ol_style_Icon.html – Chase Choi Aug 24 '18 at 13:39
  • Thanks for the suggestion @ChaseChoi I tried with icons and I could create something similar based on the requirement. As per requirement I need to implement a polygon and keep it in same size and use that as a site marker. Could you please advice? – CodeBuggy Aug 27 '18 at 12:35
  • create polygon shape icon as you tried, then use it as marker with icon style. – Chase Choi Aug 28 '18 at 02:50
  • Okay @ChaseChoi I am going to give a try with that. – CodeBuggy Aug 28 '18 at 07:01
  • @ChaseChoi I am unable to add a Polygon as style image. What I am looking for is to create a Polygon using the Polygon.js in Openlayers anduse that as site Marker. For example, I have to draw a polygon like arrow shape and use that as site markers for some area. Please help with this and I cannot use a image for this as per my requirement. – CodeBuggy Aug 28 '18 at 09:10

1 Answers1

0

@ChaseChoi I and Mike from Stack Exchange worked together and found the solution.

Please find the answer here

CodeBuggy
  • 429
  • 5
  • 18