arcgis for js 地图文字标注加背景颜色

addBackgroundTextSymbol = function (infoTem, lng, lat, x?: any, y?: any, fontsize?: any, radiu?: any) {

 

var selfCom = this;

selfCom.dj.dojoRequire(["esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleMarkerSymbol", "esri/symbols/PictureMarkerSymbol", "esri/symbols/SimpleFillSymbol", "esri/symbols/Font",

"esri/symbols/TextSymbol", "esri/geometry/Point", "esri/graphic", "dojo/_base/Color", "dojo/dom", "dojo/on", "bgText/bgTextGraphic",

"bgText/bgTextGraphicLayer", "dojo/domReady!"

], function (

SimpleLineSymbol, SimpleMarkerSymbol, PictureMarkerSymbol,

SimpleFillSymbol, Font, TextSymbol, Point, Graphic,

Color, dom, on, bgTextGraphic, bgTextGraphicLayer

) {

 

var _fontsize = fontsize || 10;

var _radius = radiu || 6

var _infoTem = infoTem + "";

var infoLayer = selfCom.currMap.getLayer("infoLayer");

 

var point = Point(lng, lat);

var bglineSymbol = new SimpleLineSymbol("solid", new Color("gray"), 1);

var width = (selfCom.chkstrlen(_infoTem)) * 0.5 * (_fontsize + 3) + 4; //

var height = _fontsize * 1.8;

//背景框圆角半径

var radius = _radius;

 

//设置背景框的大小

var path = "M0" + " " + radius + "L0" + " " + (height - radius) + "Q0" + " " + height + " " + radius + " " + height + "L" + (width - radius) + " " + height + "Q" + width + " " + height + " " + width + " " + (height - radius) + "L" + width + " " + radius + "Q" + width + " " + "0" + " " + (width - radius) + " " + "0L" + radius + " " + "0Q0" + " " + "0" + " " + "0" + " " + radius;

 

var bgSymbol = new SimpleMarkerSymbol();

bgSymbol.setPath(path);

bgSymbol.setColor(new Color("gray"));

bgSymbol.setOutline(bglineSymbol);

var size = Math.max(height, width);

bgSymbol.setSize(size);

var bgGraphic = new Graphic(point, bgSymbol);

bgSymbol.xoffset = x || 30;

bgSymbol.yoffset = y || 24;

infoLayer.add(bgGraphic);

 

var sfs = new SimpleFillSymbol(

SimpleFillSymbol.STYLE_SOLID,

new SimpleLineSymbol(SimpleLineSymbol.STYLE_SHORTDASHDOT),

new Color([0, 0, 255, 0.5]));

var font = new Font(fontsize + "px", Font.STYLE_NORMAL, Font.VARIANT_NORMAL, Font.WEIGHT_LIGHTER);

 

var ts = new TextSymbol(_infoTem, font, new Color([255, 255, 255]));

 

ts.setAlign(Font.ALIGN_START);

 

ts.xoffset = x || 30;

ts.yoffset = y || 20;

 

var textGraphic = new bgTextGraphic(point, ts, sfs);

 

infoLayer.add(textGraphic);

 

// selfCom.currMap.centerAndZoom(point, 16);

 

});

 

}

 

//获取包含汉字字符串长度

chkstrlen = function (str) {

var strlen = 0;

for (var i = 0; i < str.length; i++) {

if (str.charCodeAt(i) > 255) //如果是汉字,则字符串长度加2

strlen += 2;

else

strlen++;

}

return strlen;

}

效果图如下:

 

 

 

需要用的文件  见:https://download.csdn.net/download/u013236043/10813574

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值