简体   繁体   中英

openLayers styling points and line of GeoJSON file

I have a GeoJson file and I want to have different labeled circle points in my map. i want to do this to all point (Feature:'point'), but it sadly apply to my Line's Coordinates too (Feature:'LineString'), how can i solve this problem?

my Code:

const labelText = new Style({
  text : new Text({
    font: '12px Calibri,sans-serif',
    overflow: true,
    fill: new Fill({
      color: 'green'
    }),
    stroke : new Stroke({
      color :' #000',
      width : 3
    })
  })
});
labelText.getText().setText("R");


const RecStyle = [
  new Style({
    fill : new Fill({
      color:"green"
    }),
    stroke : new Stroke({
      color : 'blue',
      width : 2
    })
  }),
  labelText
];


//for drawing


CreateGeojson("myjson.json")

function CreateGeojson(url){
  console.log(`url : ${url}`);
  const myGeoJson = new VectorLayer({
    source: new VectorSource({
      format: new GeoJSON(),
      url: url
    }),
    style : RecStyle
  });
  map.addLayer(myGeoJson);
}


I expected to not see the line's Coordinates as labeled 'R', I want to see only my points in this style

Create a styles object indexed by geometry type similar to the editing styles in https://openlayers.org/en/latest/apidoc/module-ol_style_Style-Style.html

Then use a style function to select the appropriate style and any dynamic properties such a labels:

style: function(feature) {
  const style = styles[feature.getGeometry().getType()];
  const textStyle = style.getText();
  if (textStyle)
    textStyle.setText(feature.get('name'));
  }
  return style;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM