arcgis jsapi接口入门系列(1):地图

地图相关

        //地图相关demo
        mapFun: function () {
            //获取地图中心点
            let center = this.mapView.center;
            //地图中心点坐标(同地图坐标系)
            let x = center.x;
            let y = center.y;
            //地图中心点坐标(经纬度坐标系)
            //PS:此经纬度坐标是地图自动投影转换而来,因为不一定准确(投影转换不一定准)
            let longitude = center.longitude;
            let latitude = center.latitude;

            //设置地图中心点
            this.mapView.center = [113.5411, 22.2399];

            //获取地图缩放级别
            let zoom = this.mapView.zoom;
            //设置地图缩放级别
            this.mapView.zoom = 12;

            //获取地图比例尺
            let scale = this.mapView.scale;
            //设置地图比例尺
            this.mapView.scale = 5000;

            //获取地图范围
            let extent = this.mapView.extent;
            //地图范围坐标
            let xmax = extent.xmax;
            let xmin = extent.xmin;
            let ymax = extent.ymax;
            let ymin = extent.ymin;
            //地图范围宽高(注意此宽高是地图单位,而不是屏幕像素)
            let width = extent.width;
            let height = extent.height;

            //设置地图范围
            this.mapView.extent = new this.apiInstance.Extent({
                xmin: -9177882,
                ymin: 4246761,
                xmax: -9176720,
                ymax: 4247967,
                spatialReference: {
                    wkid: 102100
                }
            });

            //屏幕坐标转地图坐标
            //PS:参数1类型是ScreenPoint
            let mapPoint1 = this.mapView.toMap(new this.apiInstance.ScreenPoint(10, 10));

            //地图坐标转屏幕坐标
            let screenPoint1 = this.mapView.toScreen(mapPoint1);
        },

地图点击事件,同时图形(graphic)点击在此实现

                    //mapView绑定点击事件
                    this.mapView.on("click", function (event) {
                        this.mapView.hitTest(event).then(function (response) {
                            //图形(graphic)点击事件的实现
                            if (response.results[0]) {
                                //获取到点击的图形
                                var graphic = response.results[0].graphic;

                                //由于所有图形都在此事件,实际需求可能需要判断图形是哪个图形,或者图形所在哪个图层,可以通过uid和图层id来判断
                                let uid = graphic.uid;
                                //图层不一定有,例如在mapView的graphics下的就没有
                                if (graphic.layer) {
                                    let layerId = graphic.layer.id;
                                }
                            }
                        }.bind(this))
                    }.bind(this));

You may also like...

退出移动版