ArcGIS API for JavaScript 标注错位问题解决思路

项目需求:完成对点的符号化显示,并将点的某些信息显示在点符号的旁边。

设计

调研开始,展示数据前端一般用GraphicsLayer,再结合Graphic,PictureMarkerSymbol与TextSymbol就可以完成此工作。直接上代码

jidianjing_graphicsLayer: null,
jidianjing_text_featureLayer: null,
...
this.graphicsLayer = new GraphicsLayer({
    id: "graphicsLayer",
    elevationInfo: {
      mode: "relative-to-ground",
      offset: 3
    }
});
...
view.map.add(this.graphicsLayer);
...

let jidianjing_graphics = [];
                // 处理Graphics
                $.each(result, function (i, item) {
                    item.widgetModule = 'jidianjingWidgetModule_click';
                    var stcd = item.STCD;
                    var stnm = item.STNM;

                    let tempPoint = new Point({
                        type: "point",
                        x: item.LGTD,
                        y: item.LTTD
                    });
                    let normalsymbol = {
                        type: "picture-marker",
                        url: 'img/menu/081.png',
                        width: "18px",
                        height: "18px"
                    };
                    if (!StringUtil.isNull(item.WARN_NAME)) {
                        normalsymbol = {
                            type: "picture-marker",
                            url: 'img/menu/083.png',
                            width: "28px",
                            height: "28px"
                        };
                    } else if (!StringUtil.isNull(item.LL)) {
                        normalsymbol = {
                            type: "picture-marker",
                            url: 'img/menu/082.png',
                            width: "28px",
                            height: "28px"
                        };
                    }

                    let tempPop = new PopupTemplate({
                        title: '机电井:' + stnm,
                        content: _self.setjidianjingContentInfo
                    });

                    let tempGraphic = new Graphic({
                        geometry: tempPoint,
                        attributes: item,
                        symbol: normalsymbol,
                        popupTemplate: tempPop
                    });

                    let textGraphic = new Graphic({
                        geometry: tempPoint,
                        symbol: new TextSymbol({
                            color: "#FFF",
                            backgroundColor: new Color('red'),
                            borderLineColor: new Color("#fff"),
                            borderLineSize: 2,
                            haloColor: "black",
                            haloSize: "1px",
                            text: '水位:12 水温:11',
                            font: {
                                size: 8,
                                family: "sans-serif"
                            }
                        })
                    });
                _self.jidianjing_graphicsLayer.add(tempGraphic);
                _self.jidianjing_text_graphicsLayer.add(textGraphic);
...

效果

image
image

说明
gif图太大,aliyun拒绝了我的上传,所以只能用文字描述;在小比例尺下,符号标注和文字标注之间的距离较小,看着效果还可以,但是有会出现标注覆盖文字或者文字覆盖标注;随着比例尺的放大,符号和标注之间的距离会增大,如第二张图所示。但是拿给项目经理看的时候,不要这种效果,距离和标注与符号之间的距离不能变。so...改改改

新思路-构造FeatureLayer

查看了官网好多例子,发现只有FeatureLayer的标注和符号之间的距离是边的,因为我们拿到的数据是点数据,所以我们可以构造组成FeatureLayer的source,其实就对应ags里的Graphics;还有一个标注LabelClass,这个类就是用来显示标注的。so...又开干了。

jidianjing_featureLayer: null,
...
let tempGraphics = [];
// 处理Graphics
$.each(result, function (i, item) {
    item.widgetModule = 'jidianjingWidgetModule_click';
    var stcd = item.STCD;
    var stnm = item.STNM;

    let tempPoint = new Point({
        type: "point",
        x: item.LGTD,
        y: item.LTTD
    });
    let normalsymbol = {
        type: "picture-marker",
        url: 'img/menu/081.png',
        width: "18px",
        height: "18px"
    };
    if (!StringUtil.isNull(item.WARN_NAME)) {
        normalsymbol = {
            type: "picture-marker",
            url: 'img/menu/083.png',
            width: "28px",
            height: "28px"
        };
    } else if (!StringUtil.isNull(item.LL)) {
        normalsymbol = {
            type: "picture-marker",
            url: 'img/menu/082.png',
            width: "28px",
            height: "28px"
        };
    }

    let tempPop = new PopupTemplate({
        title: '机电井:' + stnm,
        content: _self.setjidianjingContentInfo
    });

    let tempGraphic = new Graphic({
        geometry: tempPoint,
        attributes: item,
        symbol: normalsymbol,
        popupTemplate: tempPop
    });

    let textGraphic = new Graphic({
        geometry: tempPoint,
        symbol: new TextSymbol({
            color: "#FFF",
            backgroundColor: new Color('red'),
            borderLineColor: new Color("#fff"),
            borderLineSize: 2,
            haloColor: "black",
            haloSize: "1px",
            text: '水位:12 水温:11',
            font: {
                size: 8,
                family: "sans-serif"
            }
        })
    });
    tempGraphics.push(tempGraphic);
...
// popup
let tempPop = new PopupTemplate({
    title: '机电井:' + '{STNM}',
    content: _self.setjidianjingContentInfo
});
// 标注
var labelClass = new LabelClass({
    minScale: 20000,
    symbol: {
        type: "label-3d",
        symbolLayers: [{
            type: "text", 
            material: {
                color: "white"
            },
            size: 10,
            halo: {
                color: [0, 0, 0, 0.8], 
                size: 0.5
            }
        }],
        verticalOffset:{
            minWorldLength: 20,
        }
    },
    labelPlacement: "above-center",
    labelExpression: "水位:[STTP], 水温:[STCD]"
    // labelExpressionInfo: {
    //   expression: "$feature.STNM"
    // }
});
// 构建feetureLayer并实现标注
this.jidianjing_text_featureLayer = new FeatureLayer({
    id: 'jidianjing_featureLayer',
    fields: [{
        name: "STCD",
        alias: "STCD",
        type: "oid"
    }, {
        name: "LGTD",
        alias: "LGTD",
        type: "string"
    }, {
        name: "LTTD",
        alias: "LTTD",
        type: "string"
    }, {
        name: "STCD",
        alias: "STCD",
        type: "string"
    }, {
        name: "STNM",
        alias: "STNM",
        type: "string"
    }, {
        name: "STTP",
        alias: "STTP",
        type: "string"
    }, {
        name: "widgetModule",
        alias: "widgetModule",
        type: "string"
    }],
    objectIdField: "STCD",
    geometryType: "point",
    spatialReference: {
        wkid: 4326
    },
    source: tempGraphics, //  an array of graphics with geometry and attributes
    // popupTemplate and symbol are not required in each graphic
    // since those are handled with the popupTemplate and
    // renderer properties of the layer
    popupTemplate: tempPop,
    // renderer: iconSymbolRenderer,
    maxScale: 0,
    minScale: 0,
    labelsVisible: true,
    labelingInfo: [labelClass]
});
view.map.add(this.jidianjing_text_featureLayer);

效果

image

image
说明

可以在上面两张图中看到,标注和符号的位置一直都是固定的,并没有随着距离的变化而变化;这时候拿给项目经理,就这个效果还算满意。

总结

符号和标注之间的距离在GraphicsLayer和FeatureLayer中的表现是不一样的,所以要根据合适的场景进行选择取舍。另外,GraphicsLayer和FeatureLayer标注换行不能实现,对GraphicsLayer可以采用加多个TextSymbol的GraphicsLayer,设置GraphicsLayer相对地面的高度就可以实现,但是距离永远是个问题,在此给一些朋友一些提醒。

优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/339363

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Mario,低调大师唯一一个Java游戏作品

Mario,低调大师唯一一个Java游戏作品

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。