ArcGIS JS API popup弹窗

*使用ArcGIS JS API 4.19

一、要素服务popup

原始弹窗由popup微件控制,view对象都自带默认的popup,格式可以由Featurelayer的popupTemplate属性控制。

测试代码(Vue中):

<template>
    
        <div id="viewDiv">
                <InfoWindow v-show="this.showWindow"></InfoWindow>
        </div>
        
</template>

<script>
import {loadModules} from 'esri-loader'


export default {
    name: 'Identify',
    data (){
        return {
            option :{
                url: '/4.19/init.js',
                css: '/4.19/esri/themes/light/main.css'
            },
            cdUrl: " ",
            currentView: {},
            
        }
    },
    
    methods :{
        _createView (){
            const self =this;
            loadModules(["esri/Map",
                "esri/views/MapView",
                "esri/layers/MapImageLayer",
                "esri/layers/FeatureLayer",
                 "esri/config"],self.option)
            .then(([Map,MapView,MapImageLayer,FeatureLayer,esriConfig])=>{
                const cdUrl = "http://localhost:6080/arcgis/rest/services/xzqhFS/FeatureServer/0";
                self.cdUrl = cdUrl;

                const cdLayer = new FeatureLayer({
                    url: cdUrl,
                    popupTemplate: {
                        title: "{DISTRICT}",
                        lastEditInfoEnabled: false,
                        content: [
                        {
                            type: "fields",
                            fieldInfos: [
                            {
                                fieldName: "DISTRICT"
                            }
                            ]
                        }
                        ]
                    }
                })
                const map = new Map({
                    basemap: 'satellite'
                });

                map.add(cdLayer);

                const view = new MapView({
                    map: map,
                    container: "viewDiv",
                    center: [104.07,30.67],
                    zoom: 7
                });

                self.currentView = view;

            }).catch((e)=>{
                console.log(e);
            });

        },
     
    },
    mounted (){
        this._createView ();
    },

}
</script>

<style lang='stylus' scoped>
    #viewDiv
        width: 100%
        height: 100%

</style>

效果:
在这里插入图片描述
另外,可以自定义一些按钮和事件,通过actions控制,参考官网示例:
https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=popup-actions
https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=popup-custom-action

二、 动态地图服务popup

动态地图服务MapImageLayer是没有popTemplate属性的,所以可能需要自己写弹窗来显示信息。

测试代码(Vue中):

<template>
    
        <div id="viewDiv">
                <InfoWindow v-show="this.showWindow"></InfoWindow>
        </div>
        
</template>

<script>
import {loadModules} from 'esri-loader'
import InfoWindow from '@/components/InfoWindow'

export default {
    name: 'Identify',
    data (){
        return {
            option :{
                url: '/4.19/init.js',
                css: '/4.19/esri/themes/light/main.css'
            },
            cdUrl: " ",
            currentView: {},
            showWindow: false,
            sPX: 0,
            sPY: 0,
            geometryArr: []
        }
    },
    components:{
        InfoWindow
    },
    
    methods :{
        _createView (){
            const self =this;
            loadModules(["esri/Map",
                "esri/views/MapView",
                "esri/layers/MapImageLayer"],self.option)
            .then(([Map,MapView,MapImageLayer])=>{
                const cdUrl = "http://localhost:6080/arcgis/rest/services/mapcd/MapServer/";
                self.cdUrl = cdUrl;
                const cdLayer = new MapImageLayer({
                    url: cdUrl
                })
                const map = new Map({
                    basemap: 'osm'
                });

                map.add(cdLayer);

                const view = new MapView({
                    map: map,
                    container: "viewDiv",
                    center: [104.07,30.67],
                    zoom: 7
                });

                self.currentView = view;

                view.when(function(){
                    // executeIdentifyTask() is called each time the view is clicked\
                    // view.on("click",self.executeIdentifyTask);

                    view.on("click",function (event) {
                        self.executeIdentifyTask(event);

                        //self.queryFeature(event);
                    });
                    
                });

            }).catch((e)=>{
                console.log(e);
            });

        },

        // Executes each time the view is clicked
        executeIdentifyTask (event){
            //console.log(event);
            const self = this;
            // Set the geometry to the location of the view click\
            loadModules(["esri/views/MapView",
                        "esri/tasks/IdentifyTask",
                        "esri/Graphic",
                        "esri/tasks/support/IdentifyParameters"],self.option)
                        .then(function([MapView,IdentifyTask,Graphic,IdentifyParameters]){
                             
                       // Create identify task for the specified map service
                            console.log('event',event);
                            console.log(self.cdUrl);
                           const identifyTask = new IdentifyTask(self.cdUrl);
                            // Set the parameters for the Identify
                            const params = new IdentifyParameters();
                            params.tolerance = 3;
                            params.layerIds = [0];
                            params.layerOption = "top";
                            params.width = self.currentView.width;
                            params.height = self.currentView.height;
                            params.returnGeometry = true; 
                            // Set the geometry to the location of the view click
                            params.geometry= event.mapPoint;
                            console.log('point',event.mapPoint);
                            params.mapExtent = self.currentView.extent;
                            console.log(params);

                            //transform mappoint to screen point
                            let sPoint = self.currentView.toScreen(event.mapPoint);
                            //获取弹出框窗口的屏幕位置
                            self.sPX = sPoint.x +50;
                            self.sPY = sPoint.y -40;
                            console.log('spoint',sPoint);

                            document.getElementById("viewDiv").style.cursor="wait";

                            // This function returns a promise that resolves to an array of features
                            // A custom popupTemplate is set for each feature based on the layer it
                            // originates from
                            identifyTask.execute(params)
                                .then(function(res){
                                    const results = res.results;
                                    console.log('results',results);
                                    if (results.length>0){
                                        self.showWindow =true;
                                    }else if(results.length==0){
                                        self.showWindow =false;
                                    }
                                    
                                    self.geometryArr = [];
                                    return results.map(function(result){
                                        
                                        const feature = result.feature;
                                        //获取存储点击要素的geometry
                                        self.geometryArr.push(feature.geometry);

                                        //定义feature的template内容
                                        console.log('info',feature);
                                        let info  = feature.attributes.COUTRICT;
                                        console.log(info);

                                        feature.popupTemplate ={
                                            title: "行政区",
                                            content: info
                        
                                        };

                                        return feature;
                                    });
                                    
                                }).then(showPopup);// Send the array of features to showPopup()

                            // Shows the results of the Identify in a popup once the promise is resolved
                            function showPopup(response) {
                                //定义获取弹窗的位置
                                if (response.length > 0) {
                                    let temp = document.getElementById("test");
                                    temp.innerHTML = `<p>${response[0].popupTemplate.title}</p><br>
                                        <p>${response[0].popupTemplate.content}</p>
                                    `;
                                    temp.style.textAlign="center";
                                    temp.style.left = `${self.sPX}px`;
                                    temp.style.right = `${self.sPX}px`;
                                }

                                //获取点击要素geometry高亮显示
                                self.currentView.graphics.removeAll();
                                if (event && self.geometryArr.length) {
                                    console.log(888,self.geometryArr);
                                    self.geometryArr.forEach(function(geometry){
                                         self.currentView.graphics.add(
                                                
                                            new Graphic({
                                                geometry: geometry,
                                                symbol: {
                                                type: "simple-fill",
                                                style: "none",
                                                outline: {
                                                    color: "#6600FF",
                                                    width: 2
                                                }
                                                }
                                            })
                                        );
                                    });
                                   
                                }
                                
                                document.getElementById("viewDiv").style.cursor = "auto";
                            }

            }).catch((e)=>{
                console.log(e);
            });
        }
        
    },
    mounted (){
        this._createView ();
    },

}
</script>

<style lang='stylus' scoped>
    #viewDiv
        width: 100%
        height: 100%

</style>

自定义窗口组件:

<template>
  <div id="test" class="testWindow">22222</div>
</template>

<script>
export default {

}
</script>

<style lang='stylus' scoped>
    .testWindow
        position: absolute
        top: 100px
        right: 30px
        width: 200px
        height: 200px
        background-color: white

</style>

效果:
在这里插入图片描述

三、其他参考

弹窗统计数据:https://developers.arcgis.com/javascript/latest/sample-code/sandbox/index.html?sample=popuptemplate-promise

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孙同学的一个笔记本

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值