I'm aware of binding a pop-up to ESRI's L.esri.DynamicMapLayer
here. The following code below is successful.
$.ajax({
type: 'GET',
url: url + '?f=json',
data: { layer: fooType },
dataType: 'json',
success: function(json) {
var foo_layer = fooLayers[fooType].layers;
foo = L.esri.dynamicMapLayer({
url: url,
layers: [foo_layer],
transparent: true
}).addTo(map).bringToFront();
foo.bindPopup(function(error, featureCollection) {
if (error || featureCollection.features.length === 0) {
return false;
} else {
var obj = featureCollection.features[0].properties;
var val = obj['Pixel Value'];
var lat = featureCollection.features[0].geometry.coordinates[1];
var lon = featureCollection.features[0].geometry.coordinates[0];
new L.responsivePopup({
autoPanPadding: [10, 10],
closeButton: true,
autoPan: false
}).setContent(parseFloat(val).toFixed(2)).setLatLng([lat, lon]).openOn(map);
}
});
}
});
But rather than a click
response I am wondering as to whether you can mouseover
using bindTooltip
instead on a dynamic map. I've looked at the documentation for L.esri.DynamicMapLayer
which says it is an extension of L.ImageOverlay
. But perhaps there is an issue outlined here that I'm not fully understanding. Maybe it is not even related.
Aside, I've been testing multiple variations of even the simplest code to get things to work below but have been unsuccessful. Perhaps because this is asynchronous behavior it isn't possible. Looking for any guidance and/or explanation(s). Very novice programmer and much obliged for expertise.
$.ajax({
type: 'GET',
url: url + '?f=json',
data: { layer: fooType },
dataType: 'json',
success: function(json) {
var foo_layer = fooLayers[fooType].layers;
foo = L.esri.dynamicMapLayer({
url: url,
layers: [foo_layer],
transparent: true
}).addTo(map).bringToFront();
foo.bindTooltip(function(error, featureCollection) {
if (error || featureCollection.features.length === 0) {
return false;
} else {
new L.tooltip({
sticky: true
}).setContent('blah').setLatLng([lat,lng]).openOn(map);
}
});
}
});
Serendipitously, I have been working on a different problem, and one of the byproducts of that problem may come in handy for you.
Your primary issue is the asynchronous nature of the click event. If you open up your map (the first jsfiddle in your comment), open your dev tools network tab, and start clicking around, you will see a new network request made for every click. That's how a lot of esri query functions work - they need to query the server and check the database for the value you want at the given
latlng
. If you tried to attach that same behavior to amousemove
event, you'll trigger a huge number of network requests and you'll overload the browser - bad news.One solution of what you can do, and its a lot more work, is to read the pixel data under the cursor of the image returned from the esri image service. If you know the exact rgb value of the pixel under the cursor, and you know what value that rgb value corresponds to in the map legend, you can achieve your result.
Here is a working example
And Here is the codesandbox source code. Don't be afraid to hit refresh, CSB is little wonky in the way it transpiles the modules.
What is happening here? Let's look step by step:
load
,zoomend
,moveend
, a specialized function is fetching the same image that L.esri.dynamicMapLayer does, using something calledEsriImageRequest
, which is a class I wrote that reuses a lot of esri-leaflet's internal logic:An instance of
EsriImageRequest
has thefetchImage
method, which takes an array ofL.LatLngBounds
and a map zoom level, and returns an image - the same image that your dynamicMapLayer displays on the map.EsriImageRequest
is probably extra code that you don't need, but I happen to have just run into this issue. I wrote this because my app runs on a nodejs server, and I don't have a map instance with anL.esri.dynamicMapLayer
. As a simpler alternative, you can target the leaflet DOM<img>
element that shows your dynamicMapLayer, use that as your image source that we'll need in step 2. You will have to set up a listener on thesrc
attribute of that element, and run theapplyImage
in that listener. If you're not familiar with how leaflet manages the DOM, look into your elements tab in the inspector, and you can find the<img>
element here:I'd recommend doing it that way, and not the way my example shows. Like I said, I happened to have just been working on a sort-of related issue.
position
,pointer-events
, andopacity
properties, it lays exactly over the map, but is set to take no interaction (I gave it a small amount of opacity in the example, but you'd probably want to set opacity to 0). In theapplyImage
function, the image we got is written to that canvas:Now we have an invisible canvas who's pixel content is exactly the same as the dynamicMapLayer's.
mousemove
event, and get the mouse's rgba pixel value from the canvas we created. If you read into my other question, you can see how I got the array of legend values, and how I'm using that array to map the pixel's rgba value back to the legend's value for that color. We can use the legend's value for that pixel, and set the popup content to that value.As you can see, I'm also setting the
latlng
of the popup to wherever the mouse is. WithcloseButton: false
in the popup options, it behaves much like a tooltip. I tried getting it to work with a properL.tooltip
, but I was having some trouble myself. This seems to create the same effect.Sorry if this was a long answer. There are many ways to adapt / improve my code sample, but this should get you started.