How to prevent a user from dropping a marker/pin/point on to a map if the map is zoomed out?

419
2
02-26-2020 04:29 AM
SiyabongaKubeka
Occasional Contributor

Hi All,

I have a map that allows a user to drop a pin/point.marker on to it.  Now what I want is to prevent a user from dropping a pin if the map is zoomed out, for example:

 

if(view.zoom >=11)

{

   //allow the user to drop pin

}

else

{

      alert("please zoom in");

}

Where would I implement this if/else statement on the following code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="initial-scale=1,maximum-scale=1,user-scalable=no"
/>
<title>DEA GIS APPLICATION</title>

<link
rel="stylesheet"
href="https://js.arcgis.com/4.12/esri/themes/light/main.css"
/>
<script src="https://js.arcgis.com/4.12/"></script>

<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<script>
require([
"esri/widgets/Sketch",
"esri/Map",
"esri/layers/GraphicsLayer",
"esri/views/MapView"
], function(Sketch, Map, GraphicsLayer, MapView) {
const layer = new GraphicsLayer();

const map = new Map({
basemap: "streets",
layers: [layer]
});

const view = new MapView({
container: "viewDiv",
map: map,
zoom: 5,
center: [90, 45]
});

var symbol = {
type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
style: "circle",
color: "blue",
size: "8px", // pixels
outline: { // autocasts as new SimpleLineSymbol()
color: [ 255, 255, 0 ],
width: 1 // points
}
};

const sketch = new Sketch({
layer: layer,
view: view,
symbol: symbol,
availableCreateTools: ["point"]
});

view.ui.add(sketch, "top-right");

sketch.on('create', function(evt){
console.log("X = ", evt.graphic.geometry.x);
console.log("Y = ", evt.graphic.geometry.y);
});
});
</script>
</head>

Tags (2)
0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

This should work.

      sketch.on('create', function (evt) {
        if (view.zoom >= 11) {
          //allow the user to drop pin
          console.log("X = ", evt.graphic.geometry.x);
          console.log("Y = ", evt.graphic.geometry.y);
        } else {
          alert("please zoom in");
          evt.graphic.layer.remove(evt.graphic);
        }
      });
0 Kudos
SiyabongaKubeka
Occasional Contributor

Thank you very much Robert.

0 Kudos