use variable for distance in esri leaflet nearby??

1076
2
Jump to solution
11-21-2020 11:15 AM
by Anonymous User
Not applicable

Hello:

How can I use a variable when using the nearby method on a query layer so the user can select the distance in a dropdown or is this not possible?

 

Here is a snippet of what I have:

HTML

<select id="buffer" name="buffer">
<option value="60.96">200 feet</option>
<option value="152.4">500 feet</option>
<option value="213.36">700 feet</option>
<option value="304.8">1000 feet</option>
</select>

JAVASCRIPT

var distance = document.getElementById("buffer").value;

 

function myFunction() {
if ((document.getElementById("pm1").checked == true) && (document.getElementById("tm1").checked == true)&& (document.getElementById("sr1").checked == true)){
pmquery.nearby(latlng, distance);

pmquery.run(function (error, featureCollection, response) {
if (error) {
console.log(error);
return;
}
L.geoJSON(featureCollection).addTo(map);
console.log('Found ' + featureCollection.features.length + ' features');
map.fitBounds(L.geoJSON(featureCollection).getBounds());
});

 

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
GavinRehkemper
Esri Contributor

Yes that code looks like it should work. Make sure you check if your "distance" variable is actually defined within your function. You can use console.log to see if the value is defined. As a quick attempt, you could also move the line

var distance = document.getElementById("buffer").value;

 within your function "myFunction"

View solution in original post

Tags (2)
0 Kudos
2 Replies
GavinRehkemper
Esri Contributor

Yes that code looks like it should work. Make sure you check if your "distance" variable is actually defined within your function. You can use console.log to see if the value is defined. As a quick attempt, you could also move the line

var distance = document.getElementById("buffer").value;

 within your function "myFunction"

Tags (2)
0 Kudos
by Anonymous User
Not applicable

Thanks Gavin. I had to make it a global variable and use parseFloat to convert from string to integer. I have another question though about how to use onEachFeature to get the attribute for all features when using query.run. I will post it later.

0 Kudos