3

I am using Leafletjs. Currently its pretty straight forward, I have a streets view from open maps.

var streets = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors'
    });

I also have a WMS layer that is coming from a geoserver. It has the standard getFeatureInfo and everything shows up correctly.

L.tileLayer.wms("GEOSERVERURL", {
            layers: 'layers',
            format: 'image/png'
            ,transparent: true
        }).addTo(map);

The wms layer is also clickable and I use getFeatureInfo to get the info for that layer. The issue is that the user doesn't know its clickable because the cursor never changes when they hover of the wms layer. My question is how do make the cursor change when hovering over the layer?

Has anyone implemented this feature before or have an idea to implement it? The only research I have stumbled across so far has using mouseover on the map and calling getFeatureInfo to tell if its over a layer. However, that seems like it would cause a lot of chatter just to identify cursor area.

EDIT: To clarify, I want the cursor to only change when its hovered over the wms layer that is populated. Although it technically gets applied to the whole map, it only has content on a part of it. Which kind of raises the question of 'Can I limit the wms layer to only the content area and then show a cursor?' Maybe a bounding area or something along those lines?

EDIT 2: Below is an example of what it looks like. The street map parts I want to keep the normal cursor but I want a pointer when hovering over the colored wms map parts. Example

Kalel Wade
  • 7,742
  • 3
  • 39
  • 55

1 Answers1

0

Set an ID on the tileLayer's container and then use CSS to change the cursor:

Javascript:

var wms = L.tileLayer.wms("GEOSERVERURL", {
    layers: 'layers',
    format: 'image/png',
    transparent: true
}).addTo(map);

wms.getContainer().setAttribute('id', 'wmsContainer');

Stylesheet:

#wmsContainer {
    cursor: grab; /* or any other cursor: https://developer.mozilla.org/en-US/docs/Web/CSS/cursor */
}

Note: you need to do this after the layer is added to the map. Before you add it to the map the getContainer method will return undefined.

Edit after question edit and comments:

Unfortunatly that's not possible. At least not as far as i know. Because L.TileLayer.WMS is a layer of images, there is absolutely no way of deducting which tiles have features on them and which are transparent.

What you could do as a workaround is work out the boundaries of your object, use that to create a transparent polygon without stroke and put that over your WMS layer. Polygons are interactive thus you get the cursorchange included, plus as an extra bonus, you can do other fancy stuff like show the outline or something like that on mouseover. I've created a little demo from the WMS example you supplied in the comments.

http://plnkr.co/edit/1HGn6IUzdrn1N5KGazXQ?p=preview

Note that i'm using a GeoJSON layer with one feature instead of a polygon, because it was easier to find the outline of the US in GeoJSON format. But in your case a four point polygon would do the trick just as wel.

Hope that helps, let me know if something isn't clear.

iH8
  • 27,722
  • 4
  • 67
  • 76
  • Close. It does change the cursor as expected except it does it for the entire map. I updated my question with some clarification. – Kalel Wade Feb 23 '15 at 18:04
  • Could you perhaps share the `GEOSERVERURL` (or something similar) so i can do some testing? – iH8 Feb 23 '15 at 18:06
  • Unfortunately no, the client won't allow it. You could use this https://www.mapbox.com/mapbox.js/example/v1.0.0/wms/ as an example though. In that case, If they hover over anything that is not the base map, a cursor will show up. So for the temp layer, it would be the entire US. – Kalel Wade Feb 23 '15 at 18:13
  • I've edited my question with a possible solution to your problem – iH8 Feb 23 '15 at 20:54
  • Ya, from everything I have been searching, it seems that it is not something that is automatically done, mainly because like you said, the wms returns images so there is no way to know where the layer is. The only way to do it seems to be by getting some data to mark the boundaries. Its something that I will need to look into more. Ideally I would like it to work similar to googles kml feature. https://developers.google.com/maps/documentation/javascript/examples/layer-kml. They are returning images back like a standard WMS but also return the bounding area it looks like and auto handling it. – Kalel Wade Feb 23 '15 at 21:04
  • Just found this plugin on the Leaflet site: https://github.com/heigeo/leaflet.wms it has lots more functionality but skimming over it i don't think it's gonna help you out with your specific request, but i could be wrong. I'll look into it later when i've got some more time. – iH8 Feb 23 '15 at 21:20
  • 1
    I'm looking for the same functionality. Has any progress or updates on getting this cursor switch for hovering over WMS tiles with data present been made? – jbukoski Aug 10 '17 at 08:57