您的位置:首页 > Web前端 > JavaScript

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

2011-10-20 16:52 627 查看
今天说一下要素类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));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: