ArcGIS Server Rest for Javascript API 学习教程-3

今天说一下要素类Graphic,这个类比较简单,它由4个部分组成:

1.geometry:构成这个要素的几何对象,可以是point/polyline/polygon等等

2.symbol:要表现为何种符号:可以是任何继承自esri.symbol.Symbol的类

3.attribute:这个要素的属性:是一个object

4.infoTemplate:单击此要素时,显示infoWIndow的模板,分两部分:title和content


顺便讲一下infoTemplate的使用技巧:

InfoTemplate的构造函数esri.InfoTemplate(title, content)

1.我们可以给content中的每个字段进行格式化,比如有一个字段是日期,我们希望把它转换成“年-约月-日”的格式,就可以自定一个函数来进行处理。

var infoTemplate = new esri.InfoTemplate();
                infoTemplate.setTitle("Population in ${NAME}");
                infoTemplate.setContent("<b>2007: </b>${POP2007:compare}<br/>");

        function compare(value, key, data){
                //此方法针对传入字段(key)对应的值(value)进行处理,在这里指的是POP2007字段,data是feature的attributes
                data:graphic的attributes
        }
当然,为了简化操作,api提供了几个常用的格式化字段的函数:DateString,DateFormat,NumberFormat
用法exp:${EVENT_DATE:DateString(local: true, hideTime: true)}

2.不但可以针对某一个字段进行格式化,我们还可以针对整个content进行格式化:

通过设置content为一个function(graphic){}自定义函数就可实现

该函数返回一个字符串、dom节点、deferred(dojo的延迟加载对象)

注意:Popup类型的InfoWindow,不支持用deferred设置其对应InfoTemplate的content


下面说一个技巧。大家知道,graphic被绘制到地图上是通过vml、canvas、svg来实现的。若有成千上万个graphic(polyline 或 polygon类型的),逐个遍历绘制是非常慢的。

若想提高效率,可以合并成一个polygon或polyline则绘制的更快,但代价是无法访问每个成员的属性

代码:

function showResults(featureSet) {
  //get features from result feature set
  var features = featureSet.features;
  //create single combined polygon
  var polygon = new esri.geometry.Polygon(featureSet.spatialReference);
  //create variable for individual feature rings
  var rings;
  for (var i=0, il=features.length; i<il; i++) {
    //assign variable to the rings array of the feature geometry
    rings = features[i].geometry.rings;
    for (var r=0, rl=rings.length; r<rl; r++) {
      //add ring to combined polygon
      polygon.addRing(rings[r]);
    }
  }
  
  //create and add graphic using combined polygon
  map.graphics.add(new esri.Graphic(polygon, symbol));
}


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值