1

I am trying to get some polygon features drawn on my map. point features are working fine. even the coordinates of my polygon are correct, i draw them as point features on my map to check that.

i am using the latest version of openlayers (3.2 i guess).

here the code i am using for populating the polygons:

mapView = new ol.View({
      center: [15.438445,47.071028],
      projection: 'EPSG:4326',
      zoom: 17
    });

            // MAP ERSTELLEN
            map = new ol.Map({
                target: 'map',
      layers: [
        new ol.layer.Group({
          'title': 'Basis',
          layers: [
            new ol.layer.Tile({
                        title: 'Bing',
                        type: 'base',
                        source: new ol.source.BingMaps({
                        key:
        'my key here ..',
                        imagerySet: 'Road'
                        }),
                    })
          ]
        }),
        window.overlayGroup
      ],
      view: mapView
            });
    sehenswuerdigkeitLayer = new ol.layer.Vector({
            title: 'Sehenswürdigkeiten',
            type: 'overlay',
      projection: 'EPSG:4326',
            source: new ol.source.Vector()
        }); 

....

  var polygonGeometry = new ol.geom.Polygon();
          var coordinates = [];

          console.log(geoJson.coordinates);
          $.each(geoJson.coordinates, function(i, polyCoordsArr) {

            $.each(polyCoordsArr, function(i2, polyCoords) {
              if (polyCoords.length == 0) {
                return true;
              }

              var geschaeftFeature = new ol.Feature({
                geometry: new ol.geom.Point(
                  [polyCoords[1],polyCoords[0]]
                )
              });
              sehenswuerdigkeitLayer.getSource().addFeature(geschaeftFeature);

              coordinates.push([polyCoords[1],polyCoords[0]]);
            });
          });

          console.log(coordinates);



          polygonGeometry.setCoordinates(coordinates);
          sehenswuerdigkeitFeature.setGeometry(polygonGeometry);
sehenswuerdigkeitLayer.getSource().addFeature(sehenswuerdigkeitFeature);

the loops are neccessary because i get those coordinates from my database, also i have to switch those coordinates since openlayers use [longitude,latitude] and not [latitude, longitude] as point.

the result of my code gives following object output:

coordinates object

here you can see that the points / coordinates of my polygon are valid (visible as blue circles, ignore the green round circles, thats just other features)

map preview with polygon coordinates displayed as point feature

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.