Arcgis api for JavaScript学习笔记之Graphic

1、Class: Graphic

A Graphic can contain geometry, a symbol, attributes, or an infoTemplate. A Graphic is displayed in the GraphicsLayer. The GraphicsLayer allows you to listen for events on Graphics.//Graphic是一个超类,是geometry、symbol、attribute类的集合类。是常用的,便捷的。

构造器:new Graphic(geometry?, symbol?, attributes?, infoTemplate?)/new Graphic(json)

2、Class: GraphicsLayer

A layer that contains one or more Graphic features//用于承接Graphic要素的图层,总是位于动态图层和切片栅格图层之上,地图容器默认带一个,通过Map.graphics调用,也可以自定义创建new GraphicsLayer(),是FeatureLayer的父类。

PS:GraphicsLayer和普通的图层一样,如果要在地图上显示的话,需要map.addLayer(graphicLayer);

3、Class: Geometry

The base class for geometry objects. This class has no constructor.default spatial reference of 4326 if one is not explicitly provided in the constructor.//几何对象基类,默认空间参考4326,geometry只是几何图形的对象,不包含属性信息,属性信息包含在graphic中

子类:ExtentMultipointPointPolygonPolyline

4、Class: FeatureSet

A collection of features//是要素的数组,可以用来承接生成的多个graphic对象。

  var graphic = new Graphic( ... );
 
var features= [];
  features
.push(graphic);
 
var featureSet = new FeatureSet();
  featureSet
.features = features;


实例应用:

1.获取GraphicLayer的Graphic对象数组

       var point1 = new Point(111.1, 29.8, new SpatialReference({ wkid: 4326 }));//创建点对象,是geometry对象
       var point2 = new Point(112.1, 29.8, new SpatialReference({ wkid: 4326 }));
       var graphic1 = new Graphic(point1, new SimpleMarkerSymbol());//创建包含point1的graphic对象
       var graphic2 = new Graphic(point2, new SimpleMarkerSymbol());
       var graphicLayer = new GraphicsLayer();//创建graphicLayer对象,用来承接展示创建的graphic对象
       graphicLayer.add(graphic1);//将grapic对象添加到graphiclayer图层中
       graphicLayer.add(graphic2);
       map.addLayer(graphicLayer);//将graphiclayer图层加载到map(地图)容器内显示
      //创建featureSet对象,该对象用来可以简单理解为graphic对象的数组对象
       var featureSet = new FeatureSet();
      //调用graphicLayer的graphics属性(Properties),该属性的类型是Graphic[],返回的是改graphicLayer图层所包含的Graphic对象数组
       var g=graphicLayer.graphics;
      //再将这个Graphic[]赋予FeatureSet对象,featureSet的features属性同样是Graphic[]类型,可以用于承接GraphicLayer上返回的Graphic[]
       featureSet.features=g;
      //现在featureSet中就包含了graphic1,和graphic2了

2.从FeatureSet对象调用Graphic对象或直接从GraphicLayer图层(对象)调用Graphic对象

        //1)g1即是featureSet中包含的第一个Graphic对象
        var g1=featureSet.features[0];
        //featureLyer是特殊的GraphicLayer,是其子类
        var g2=featureLayer.graphics[0];
        var g3=graphicLayer.graphics[0];
        //2)通过遍历调用
        dojo.forEach(featureSet.features, function (feature) {
                  ...
         });

PS:调用时应注意要现实例化图层对象之后,才能对使用图层对象的方法(如使用FeatureLayer的graphics属性,featureLayer.graphics)。也就是最好不要同时实例化图层对象和使用图层对象的方法/属性,否则会出现如“graphics undefinded”之类的错误,错误的原因就是调用对象方法/属性时,由于图层对象实例化并没有反馈出来,导致不能获取实例化的图层对象,从而使调用方法/属性出错。

以上便是关于Arcgis API for JavaScript的此四个类的介绍及实例。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值