前言

关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类的介绍,还有就是在线例子:esri 官网在线例子,这个也是学习 arcgis api 3.x 的好素材。

内容概览

  1. arcgis api 3.x for js 解决 textSymbol 文本换行显示
  2. 源代码 demo 下载

arcgis api 3.x for js 默认加载 textsymbol 显示文本是不支持换行的,识别不到 \n \r 等等转义符,需要拓展一下才能支持,下面是拓展后的效果图如下:

实现思路

本篇实现文本换行显示效果 demo 是在arcgis api 3.x for js 地图加载多个 SHP 图层压缩以及 json 文件展示(附源码下载)基础上弄的

  • 拓展支持换行的 esri.symbol.MultiLineTextSymbol.js 文件,全部代码如下:
require(["esri/layers/LabelLayer"], function(ll)
{
if( typeof esri.layers.LabelLayer.prototype._addLabel == 'function' )
{
esri.layers.LabelLayer.prototype._addLabel2 = esri.layers.LabelLayer.prototype._addLabel;
esri.layers.LabelLayer.prototype._addLabel = function(a,b,c,e,g,k,m)
{
// replace \n by <br>
a = a.replace(/\n/g, "<br />");
this._addLabel2(a,b,c,e,g,k,m);
}
}
}); require(["esri/symbols/TextSymbol", "dojox/gfx/svg"], function(ts, svg)
{
if( typeof dojox.gfx.svg.Text.prototype.setShape == 'function' )
{
dojox.gfx.svg.Text.prototype.setShape = function(p)
{
this.shape = dojox.gfx.makeParameters(this.shape, p);
this.bbox = null;
var r = this.rawNode, s = this.shape;
r.setAttribute("x", s.x);
r.setAttribute("y", s.y);
r.setAttribute("text-anchor", s.align);
r.setAttribute("text-decoration", s.decoration);
r.setAttribute("rotate", s.rotated ? 90 : 0);
r.setAttribute("kerning", s.kerning ? "auto" : 0);
r.setAttribute("text-rendering", "optimizeLegibility"); while(r.firstChild)
r.removeChild(r.firstChild); if(s.text)
{
var texts = s.text.replace(/<br\s*\/?>/ig, "\n").split("\n");
var lineHeight = 1.1 * parseInt(document.defaultView.getComputedStyle(r, "").getPropertyValue("font-size"), 10);
if( isNaN(lineHeight) || !isFinite(lineHeight) )
lineHeight = 15; for(var i = 0, n = texts.length; i < n; i++)
{
var tspan = (document.createElementNS ? document.createElementNS(dojox.gfx.svg.xmlns.svg, "tspan") : document.createElement("tspan"));
tspan.setAttribute("dy", i ? lineHeight : -(texts.length-1)*lineHeight/2);
tspan.setAttribute("x", s.x);
tspan.appendChild((dojox.gfx.useSvgWeb ? document.createTextNode(texts[i], true) : document.createTextNode(texts[i])));
r.appendChild(tspan);
}
} return this;
}
}
});
  • map.html引用 esri.symbol.MultiLineTextSymbol.js
<script type="text/javascript" src="js/main/esri.symbol.MultiLineTextSymbol.js"></script>
  • map.js 加载显示核心代码:
//定义文本图层
textGraphicsLayer = new esri.layers.GraphicsLayer();//标注文本图层
map.addLayer(textGraphicsLayer);//地图添加图层
//创建textsymbol文本标注
if (data.features.length > 0) {//动态读取json数据源结果集
for (var i = 0; i < data.features.length; i++) {
var feature = data.features[i];
var point = new esri.geometry.Point(feature.geometry.x, feature.geometry.y, map.spatialReference);
//定义文本symbol
textsymbol = new esri.symbol.TextSymbol(feature.attributes.NAME + "\n" + feature.attributes.PHONE).//动态设置文本值
setColor(new dojo.Color([128, 0, 0])).//setColor设置文本颜色
setFont(new esri.symbol.Font("10pt").//setFont设置文本大小
setWeight(esri.symbol.Font.WEIGHT_BOLD)). //setWeight设置文本粗体
setOffset(0, -35);//设置偏移方向
var graphic = new esri.Graphic(point, textsymbol);
textGraphicsLayer.add(graphic);
}
}
//创建textsymbol文本标注
if (data.features.length > 0) {//动态读取json数据源结果集
for (var i = 0; i < data.features.length; i++) {
var feature = data.features[i];
var point = new esri.geometry.Point(feature.geometry.x, feature.geometry.y, map.spatialReference);
//定义文本symbol
textsymbol = new esri.symbol.TextSymbol(feature.attributes.NAME + "\n" + feature.attributes.ADDRESS).//动态设置文本值
setColor(new dojo.Color([128, 0, 0])).//setColor设置文本颜色
setFont(new esri.symbol.Font("10pt").//setFont设置文本大小
setWeight(esri.symbol.Font.WEIGHT_BOLD)). //setWeight设置文本粗体
setOffset(0, -35);//设置偏移方向
var graphic = new esri.Graphic(point, textsymbol);
textGraphicsLayer.add(graphic);
}
}

完整demo源码见小专栏文章尾部GIS之家小专栏

文章尾部提供源代码下载,对本专栏感兴趣的话,可以关注一波

arcgis api 3.x for js 解决 textSymbol 文本换行显示(附源码下载)的更多相关文章

  1. arcgis api 4.x for js 图层拓展篇之mapvLayer(附源码下载)

    因为在项目开发过程中,使用的arcgis js api版本是4.7,并不能支持客户端渲染热力图,想到arcgis js api 4.x的渲染是基于canvas,故琢磨着是否能借助类似于mapV.ech ...

  2. arcgis api 3.x for js 入门开发系列批量叠加 zip 压缩 SHP 图层优化篇(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  3. arcgis api 4.x for js 结合 react 入门开发系列初探篇(附源码下载)

    你还在使用 JQuery 或者 Dojo 框架开发 arcgis api 4.x for js 吗?想试试模块化开发吗?随着前端技术的发展,arcgis api 4.x for js 也有了结合 re ...

  4. arcgis api 3.x for js 地图加载多个 SHP 图层压缩以及 json 文件展示(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  5. arcgis api 3.x for js 入门开发系列二十二地图模态层(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  6. arcgis api 3.x for js 入门开发系列十八风向流动图(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  7. arcgis api 3.x for js 入门开发系列十七在线天地图、百度地图、高德地图(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  8. arcgis api 3.x for js 之 echarts 开源 js 库实现地图统计图分析(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  9. arcgis api 3.x for js 实现克里金插值渲染图不依赖 GP 服务(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

随机推荐

  1. Go-包

    Go-包 包的介绍以及使用 为什么使用包 为了更加好的维护代码 包的位置 必须再GOPATH路径的src中 能导入的内容 导入的内容名称必须是大写字母开头不然无法导入 包 src中的一个文件夹为一个包 ...

  2. 从华为“鸿蒙”备胎看IT项目建设

    别误会啊,本文并不在讲大家在做IT项目建设的时候学华为做一个备胎系统,以防正主系统崩掉之后能够及时替换到备胎系统里面,不影响业务. 前段时间华为被美帝制裁,然后各家组织对华为各种限制.然而华为整体布局 ...

  3. subprocess, re模块,logging, 包等使用方法

    subprocess, re模块,logging, 包等使用方法 subprocess ''' subprocess: sub: 子 process: 进程 可以通过python代码给操作系统终端发送 ...

  4. Flask报如下错误:SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.

    在同一个项目中由于flask_sqlalchemy版本不同,有时会报如下错误 错误信息如下: flask_sqlalchemy\__init__.py:: UserWarning: SQLALCHEM ...

  5. 更改 undo_retention 时,Lob retention 不更改 (Doc ID 563470.1)

    Lob retention not changing when undo_retention is changed (Doc ID 563470.1) APPLIES TO: Oracle Datab ...

  6. [Linux] nginx记录多种响应时间

    官网介绍$request_time – Full request time, starting when NGINX reads the first byte from the client and ...

  7. Centos7配置桥接网络

  8. CodeForces - 1230C(思维/暴力)

    题意 https://vjudge.net/problem/CodeForces-1230C 给了你总共有21张多米诺骨牌,每张牌有两个面,然后给你一个无向图,保证没有环和一个顶点多条边的情况存在.现 ...

  9. JVM-4-堆内存划分

    什么是堆内存划分     Java虚拟机根据对象存活的周期不同,把堆内存划分为几块,   一般分为新生代.老年代和永久代,这就是JVM的内存分代策略.(JDK 1.8之后将最初的永久代取消了,由元空间 ...

  10. prometheus监控tomcat

    下载tomcat,wget **;解压tar zxvf **; 下载jmx_exporter, wget  https://repo1.maven.org/maven2/io/prometheus/j ...