1

I'm trying to implement a CRUD that have a map OpenLayers.

  1. First, I drawed and saved the features with writeFeatures().

  2. Second, I'm trying to show the polygon that I drawed, and here is my problem:

  • After I set the map.setLayer my map doesn't show the features. I would like that when I select the product in the list, my map show the features that I read from my database, in format JSON. I saved using WriteFeatures();

  • Could I need a loop to iterate each item of array?

What am I doing wrong?

My configuration:

  • Laravel v6.18.22 with bootstrap;
  • OpenLayers v6.3.1

In my HTML has:

<div id="map" class="map" </div>

  <script>
    const getFeatur = @json(route('homeController.getLavouras'));
  </script>

 <script type="application/javascript" >
  my_map.display();
 </script>

In my external JavaScript file (my_map.js) has:

    import 'ol/ol.css';
    import Feature from 'ol/Feature';
    import Geolocation from 'ol/Geolocation';
    import Map from 'ol/Map';
    import Point from 'ol/geom/Point';
    import View from 'ol/View';
    import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style';
    import {OSM, Vector as VectorSource} from 'ol/source';
    import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
    import VectorContext from 'ol/render/VectorContext';
    import GeoJSON from 'ol/format/GeoJSON';    
var my_map = {                       // <-- add this line to declare the object
    display: function () {           // <-- add this line to declare a method 
      
      var view = new View({
        center: ol.proj.fromLonLat([-49.450270, -15.496072]),
        zoom: 6 ,
      });
      
      var map = new Map({
        layers: [
          new TileLayer({
            source: new OSM(),
          }) ],
        target: 'map',
        view: view,
      });
      
      new VectorLayer({
        map: map,
        source: new VectorSource()
      });
       
document.getElementById('produtoid').onchange = function(){
var produtoid = document.getElementById('produtoid').value; 
var _token = $('input[name="_token"]').val();
        $.ajax({
                        url:getFeatur,
                        method:"GET",
                        data:{produtoid:produtoid, _token:_token},
                        success:function(result)
                        {             
 
                          var newSource = new ol.source.Vector({
                                        format: new ol.format.GeoJSON(),
                                        features: new GeoJSON().readFeatures(result.dados[0].feature, 
                                            {
                                                dataProjection: 'EPSG:26918',
                                                featureProjection: 'EPSG:3857'
                                             }),
                                        });

                          
                          var nwLayer = new ol.layer.Vector({
                              title: 'My Title',
                              source: newSource,     
                              visible: true,
                          });         
                          map.addLayer(nwLayer); --> I think that here my map has to refresh and show the features. 
                          
                      }            
                  });
                }


}                                // <-- close the method
};                                   // <-- close the object
export default my_map;               // <-- and export the object

3
  • Your code contains a mix of module imports and full build syntax, so you are probably getting errors with ol.proj.fromLonLat ol.source.Vector ol.format.GeoJSON and ol.layer.Vector
    – Mike
    Commented Sep 1, 2020 at 9:58
  • But nothing errors in Console.log. When I put a return ajax variable to Console.log, I see a JSON format, exemple: {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[[[[-13027197.585166512,5415923.420611887],[-13026918.952481057,5427423.809086096],[-13028401.839417916,5434583.655001746],[-13027345.306130797,5439431.734050472],[-13025439.516448416,5440697.514279615],[-13026075.596018808,5442735.2890267335],[-13022686.028843643,5443720.071568927],[-13021837.663004309,5445899.96656019],[-13021836.772448381,5450683.865034185],[-1
    – Iram Faria
    Commented Sep 1, 2020 at 18:13
  • Mike, can you give me how most I do?
    – Iram Faria
    Commented Sep 1, 2020 at 18:58

1 Answer 1

0

There is no concept of map refreshing when it comes to OpenLayers, I think in your case style property is missing in the vector layer object

example vector layer looks like this -

  source: new VectorSource(),
  map: map,
  style: new Style({
    stroke: new Stroke({
      color: 'rgba(255, 255, 255, 0.7)',
      width: 2,
    }),
  }),
});

You can refer to this link here - Vector Layer in OpenLayers

Also, you can just update the source of the vector layer, it will reflect on the map directly, and you don't need to create a new layer every time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.