When drawing a LineString in OpenLayers, if the only thing I have a reference to is a TileWMS layer, is there any way to get the View Projection and Resolution?

I am using the drawend event, so it could come from the feature or geometry? I can't find a getView or getMap in any of these objects.

I need to query a TileWMS service and the way it is written I do not have a handle to the View or Map instance...

function drawEnd(evt) {
  let coords = evt.feature.getGeometry().getCoordinates();

  // how can i get these??
  let projection = null; // ????;
  let resolution = null; // ????;

  let getFeatureInfoUrl = self._myTileWMSLayer.getSource().getFeatureInfoUrl(evt.coordinates, resolution, projection);
  // call getFeatureInfoUrl....

Do you have access to the code wich appends '_myTileWMSLayer' to the map? If yes, you can link the map or view instance to this layer. If not do you at least have access somewhere before map inicialization or before adding this layer? if yes you could change ol.Map.prototype.addLayer so it would link the map to the layer, the definition is here but be aware that minified version looks different. But that is pretty hardcore solution, which could break OL if not written correctly and I aslo dont know if it is called when initializing map with the layer as input parameter. Easiest is to cache resolution and projection to some global variable, but that depends on your code access.

  • Ok thanks, so it sounds like there is no way to find the map from the Openlayers API given a layer, feature, or source. It's surprising layer doesn't have a reference to the map. thanks for the ideas. – user210757 May 17 '18 at 14:09

Your Answer

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

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