缓冲区分析WebMercator - 4.11.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>缓冲区分析WebMercator - 4.11</title>

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

    <link rel="stylesheet"
          href="http://localhost:8080/arcgis_js_v411_api/arcgis_js_api/library/4.11/esri/themes/light/main.css">
    <script src="http://localhost:8080/arcgis_js_v411_api/arcgis_js_api/library/4.11/init.js"></script>
    <style>
        html, body, #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }
    </style>
</head>

<body>
<div id="viewDiv">
    <div id="point-button" class="esri-widget esri-widget--button esri-interactive" title="画点">
        <span class="esri-icon-radio-checked"></span>
    </div>
    <div id="line-button" class="esri-widget esri-widget--button esri-interactive" title="画线">
        <span class="esri-icon-polyline"></span>
    </div>
    <div id="polygon-button" class="esri-widget esri-widget--button esri-interactive" title="画面">
        <span class="esri-icon-polygon"></span>
    </div>
    <div id="circle-button" class="esri-widget esri-widget--button esri-interactive" title="画圆">
        <span class="esri-icon-radio-unchecked"></span>
    </div>
    <div id="rectangle-button" class="esri-widget esri-widget--button esri-interactive" title="画矩形">
        <span class="esri-icon-checkbox-unchecked"></span>
    </div>
</div>
<script>
    require([
        "esri/Map",
        "esri/views/MapView",
        "esri/views/draw/Draw",
        "esri/Graphic",
        "esri/geometry/Polyline",
        "esri/geometry/Polygon",
        "esri/geometry/Point",
        "esri/geometry/Circle",
        "esri/geometry/geometryEngine",
        "dojo/domReady!"
    ], function (
        Map, MapView,
        Draw, Graphic,
        Polyline, Polygon, Point, Circle, geometryEngine
    ) {
        let map = new Map({
            basemap: "dark-gray"
        });
        let view2d = new MapView({
            container: "viewDiv",
            map: map,
            center: [116.397245, 39.900930],  // 经度范围:[-180, 180], 纬度范围:[-90,90]
            zoom: 8
        });
        let draw, drawAction;
        console.log("view2d:", view2d);

        // 添加各种按钮,自定义UI。
        view2d.ui.add("point-button", "top-left");
        view2d.ui.add("line-button", "top-left");
        view2d.ui.add("polygon-button", "top-left");
        view2d.ui.add("circle-button", "top-left");
        view2d.ui.add("rectangle-button", "top-left");
        view2d.ui.remove("attribution");  // 移除底部ESRI logo

        let simpleMarkerSymbol = {
            type: "simple-marker",  // autocasts as new SimpleMarkerSymbol()
            style: "square",
            color: "blue",
            size: "8px",  // pixels
            outline: {  // autocasts as new SimpleLineSymbol()
                color: [255, 255, 0],
                width: 3  // points
            }
        };
        let simpleLineSymbol = {
            type: "simple-line", // autocasts as new SimpleLineSymbol()
            color: [4, 90, 141],
            width: 4,
            cap: "round",
            join: "round"
        };
        let simpleFillSymbol_buffer = {
            type: "simple-fill", // autocasts as new SimpleFillSymbol()
            color: [140, 140, 222, 0.5],
            outline: {
                color: [0, 0, 0, 0.5],
                width: 2
            }
        };
        let simpleFillSymbol = {
            type: "simple-fill",  // autocasts as new SimpleFillSymbol()
            color: [51, 51, 204, 0.9],
            style: "solid",
            outline: {  // autocasts as new SimpleLineSymbol()
                color: "white",
                width: 1
            }
        };
        view2d.when(function () {
            draw = new Draw({
                view: view2d
            });
            document.getElementById("point-button").onclick = function () {
                view2d.graphics.removeAll();
                enableCreatePoint();
            };
            document.getElementById("line-button").onclick = function () {
                view2d.graphics.removeAll();
                enableCreateLine();
            };
            document.getElementById("polygon-button").onclick = function () {
                view2d.graphics.removeAll();
                enableCreatePolygon();
            };
            document.getElementById("circle-button").onclick = function () {
                view2d.graphics.removeAll();
                enableCreateCircle();
            };
            document.getElementById("rectangle-button").onclick = function () {
                view2d.graphics.removeAll();
                enableCreateRectangle();
            };
        });

        function enableCreatePoint() {
            let drawAction = draw.create("point");
            drawAction.on(["vertex-add", "vertex-remove", "draw-complete"], createPoint);
        }

        function createPoint(event) {
            let graphic = new Graphic({
                geometry: {
                    type: "point", // autocasts as new Point()
                    x: event.coordinates[0],
                    y: event.coordinates[1],
                    spatialReference: view2d.spatialReference
                },
                symbol: simpleMarkerSymbol
            });
            // 将绘制的图形添加到view
            view2d.graphics.add(graphic);
            if (event.type == "draw-complete") {
                console.log("point:", graphic.geometry);
                createBuffer(graphic.geometry);
            }
        }

        function enableCreateLine() {
            let drawAction = draw.create("polyline", {
                mode: "click"
            });
            // 监听各种事件,绘制折线。
            drawAction.on(["vertex-add", "vertex-remove", "cursor-update", "draw-complete"], createPolyline);
        }

        function createPolyline(event) {
            view2d.graphics.removeAll();
            let graphic = new Graphic({
                geometry: {
                    type: "polyline", // autocasts as new Polyline()
                    paths: event.vertices,
                    spatialReference: view2d.spatialReference
                },
                symbol: simpleLineSymbol
            });
            view2d.graphics.add(graphic);
            if (event.type == "draw-complete") {
                console.log("polyline:", graphic.geometry);
                createBuffer(graphic.geometry);
            }
        }

        function enableCreatePolygon() {
            let drawAction = draw.create("polygon", {
                mode: "click"
            });
            drawAction.on(["vertex-add", "vertex-remove", "cursor-update", "draw-complete"], createPolygon);
        }

        function createPolygon(event) {
            view2d.graphics.removeAll();
            let vertices = event.vertices;
            let vertices2 = vertices.slice();
            vertices2[vertices2.length] = vertices2[0];
            let graphic = new Graphic({
                geometry: {
                    type: "polygon", // autocasts as new Polygon()
                    rings: vertices2,
                    spatialReference: view2d.spatialReference
                },
                symbol: simpleFillSymbol
            });
            // 将绘制的图形添加到view
            view2d.graphics.add(graphic);
            if (event.type == "draw-complete") {
                console.log("polygon:", graphic.geometry);
                createBuffer(graphic.geometry);
            }
        }

        function createBuffer(geometry) {
            /*此函数用于,构造缓冲区。
            * geometry, 用于构造缓冲区的几何图形。
            * 注意:当前视图的spatialReference是,isWebMercator: true,
            * 所以使用geometryEngine.geodesicBuffer方法!*/
            let buffer;
            if (view2d.spatialReference.isWGS84 || view2d.spatialReference.isWebMercator) {
                // WGS84 (wkid: 4326,地理坐标系) 或 Web Mercator(投影坐标系)坐标系。
                console.log("WGS84 或 Web Mercator坐标系。");
                buffer = geometryEngine.geodesicBuffer(geometry, 10, "kilometers");
            }
            else if (view2d.spatialReference.isGeographic) {
                // 地理坐标系。
                // GeometryService.buffer();
            }
            else {
                // 投影坐标系。
                buffer = geometryEngine.buffer(geometry, 10, "kilometers");
            }
            view2d.graphics.add(
                new Graphic({
                    geometry: buffer,
                    symbol: simpleFillSymbol_buffer
                })
            );
        }

        function enableCreateCircle() {
            drawAction = draw.create("circle", {
                mode: "click"  // 点击方式加点
            });
            drawAction.on(["vertex-remove", "cursor-update", "draw-complete"], createCircle);
        }

        function createCircle(event) {
            view2d.graphics.removeAll();
            let vertices = event.vertices;
            // 少于2个点无法展示圆。
            if (vertices.length < 2) {
                return
            }
            // 构造圆心点。
            let center = new Point({
                hasZ: false,
                hasM: false,
                x: vertices[0][0],
                y: vertices[0][1],
                spatialReference: view2d.spatialReference
            });
            // 求两点的距离,即,半径。
            // point.distance(), 此方法貌似只适合在“投影坐标系”中使用。
            let dis = center.distance(new Point({
                hasZ: false,
                hasM: false,
                x: vertices[1][0],
                y: vertices[1][1],
                spatialReference: view2d.spatialReference
            }));
            let circle = new Circle({
                hasZ: false,
                hasM: false,
                center: center,
                radius: dis,
                geodesic: true,
                spatialReference: view2d.spatialReference
            });
            let graphic = new Graphic({
                geometry: circle,
                symbol: {
                    type: "simple-fill",  // autocasts as new SimpleFillSymbol()
                    color: [51, 51, 204, 0.9],
                    style: "solid",
                    outline: {  // autocasts as new SimpleLineSymbol()
                        color: "white",
                        width: 1
                    }
                }
            });
            // 将绘制的图形添加到view
            view2d.graphics.add(graphic);
            if (event.type == "draw-complete") {
                // createBuffer(circle)

                console.log("vertices:", vertices);
                console.log("center:", center);
                console.log("other:", {
                    type: "point", // autocasts as new Point()
                    x: vertices[1][0],
                    y: vertices[1][1],
                    spatialReference: view2d.spatialReference
                });
                console.log("dis:", dis);
                console.log("circle:", circle);
                console.log("graphic:", graphic);
            }
        }

        function enableCreateRectangle() {
            let drawAction = draw.create("rectangle", {
                mode: "click"//点击方式加点
            });
            // 获取焦点
            // view2d.focus();

            //顶点移除事件
            drawAction.on("vertex-remove", createRectangle);
            // 鼠标移动事件
            drawAction.on("cursor-update", createRectangle);
            // 绘制完成事件
            drawAction.on("draw-complete", createRectangle);

        }

        function createRectangle(event) {
            //获取所有顶点
            let vertices = event.vertices;

            //两点画矩形
            if (vertices.length < 2) {
                return
            }
            let rings = [vertices[0], [vertices[0][0], vertices[1][1]], vertices[1], [vertices[1][0], vertices[0][1]]];
            //清除之前绘制
            view2d.graphics.removeAll();
            let polygon = new Polygon({
                hasZ: false,
                hasM: false,
                rings: [rings],
                spatialReference: view2d.spatialReference
            });
            // 生成绘制的图形
            let graphic = new Graphic({
                geometry: polygon,
                symbol: {
                    type: "simple-fill",  // autocasts as new SimpleFillSymbol()
                    color: [51, 51, 204, 0.9],
                    style: "solid",
                    outline: {  // autocasts as new SimpleLineSymbol()
                        color: "white",
                        width: 1
                    }
                }
            });
            // 将绘制的图形添加到view
            view2d.graphics.add(graphic);
            // console.log(event);
            if (event.type == "draw-complete") {
                createBuffer(polygon);
            }
        }
    })

    /*知识点:
    * 1.点、线、面等的几何图形的空间参考Default Value:WGS84 (wkid: 4326)。*/
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值