0

I'm having trouble selecting a feature inside a KML, and wondering if this is even supported.

as a pretty basic example i have:

var vectorSource = new ol.source.Vector({
    //url: 'https://developers.google.com/kml/documentation/KML_Samples.kml',
    url:'https://openlayers.org/en/latest/examples/data/kml/2012-02-10.kml',
      format: new ol.format.KML()
  });

  var vectorLayer = new ol.layer.Vector({
    source: vectorSource
  });

  var map = new ol.Map({
    layers: [
      new ol.layer.Tile({
        source: new ol.source.OSM()
      }),
      vectorLayer
    ],
    target: 'map',
    controls: ol.control.defaults({
      attributionOptions: {
        collapsible: false
      }
    }),
    view: new ol.View({
      center: [876970.8463461736, 5859807.853963373],
      projection: 'EPSG:3857',
      zoom: 10
    })
  });
  var selectClick = new ol.interaction.Select({
    condition: ol.events.click
  });
  map.addInteraction(selectClick);

which you can find running here: https://codepen.io/vishnu4/pen/oQzVVz you'll notice the select action does nothing, where i have a similar geojson example that works fine : https://codepen.io/vishnu4/pen/xQEMjy .

Is this as expected? Or is there a workaround to get this to work so i can select the specific features (placemarks?) inside the KML object?

Just as an aside, I know that getting a feature using forEachFeatureAtPixel works ( https://codepen.io/vishnu4/pen/YRGbyR ) , but I'm looking to use the Select method.

1 Answer 1

1

The select interaction is working fine and features are being selected. It's just that you don't see the selected feature being highlighted on a KML as it does with GeoJSON.

If you disable the KML styles by using extractStyles: false in ol.format.KML() you can see that select interaction works as expected. https://codepen.io/anon/pen/zMKgeZ

3
  • wow, thanks. that helps, but i really need those styles to show. Whats the best way to show a selection and see the kml as well?
    – Phil
    Nov 12, 2018 at 15:43
  • Do you mean retaining the KML styles and showing the selection? I'm not sure if there's any way to do that. Depending on what you are trying to do with the selection you may try cloning the feature and apply selection to that. Nov 12, 2018 at 17:23
  • yeah, that was what i meant. I'll look into your idea, but you've answered my original question. Thank you!
    – Phil
    Nov 12, 2018 at 17:36

Your Answer

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

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