Gis专辑gis

arcgis js 3.x版本加载geojson数据

2018-05-31  本文已影响5人  gis杭州

先看目录结构


image.png

效果图:


image.png

完整代码如下:
jianganqu.html

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
  <title>arcgisDynamicMapServiceLayer</title>
  <link rel="stylesheet" type="text/css"
        href="https://js.arcgis.com/3.20/esri/css/esri.css"/>
  <script>var dojoConfig = {
    parseOnLoad: true,
    packages: [{
      "name": "myModules",
      "location": location.pathname.replace(/\/[^/]+$/, "") + "/myModules"
    }]
  };
  </script>
  <script type="text/javascript" src="https://js.arcgis.com/3.20/init.js"></script>
  <script src="https://unpkg.com/terraformer@1.0.8"></script>
  <script src="https://unpkg.com/terraformer-arcgis-parser@1.0.5"></script>
  <style>
  html, body, #map {
      height: 100%;
      margin: 0;
      padding: 0;
    }
  </style>


  <script>
    var map;

    require(["esri/map","myModules/geojsonlayer","esri/layers/ArcGISDynamicMapServiceLayer",
      "esri/geometry/jsonUtils",  "dojo/on", "dojo/domReady!"],
      function (Map, GeoJsonLayer,ArcGISDynamicMapServiceLayer,geometryJsonUtils,on) {
      map = new Map("map", {
        zoom: 10,
        maxZoom: 18,
        // minZoom: 8,
        center: [120.243, 30.256],
        logo: false
      });

      var geoqtiledUrl = "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer";
      var geoqTiledLayer = new esri.layers.ArcGISTiledMapServiceLayer(geoqtiledUrl);
      map.addLayer(geoqTiledLayer);



      var geoJsonLayer = new GeoJsonLayer({
        url: "data/jiangganoutline.geojson"
      });
      map.addLayer(geoJsonLayer);



    });
  </script>
</head>

<body>
<div id="map"></div>
</body>
</html>

jiangganoutline.geojson (可以用GitHub上链接代替:https://lxqjss.github.io/data/jiangganoutline.geojson

{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "Id": 0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [180, 90], [180, -90], [-180, -90], [-180,90], [180,90] ], [ [ 120.23581545625348, 30.387234733170715 ], [ 120.22793061696586, 30.378649091144439 ], [ 120.22993326187134, 30.376503324259886 ], [ 120.23186445236206, 30.377671479655078 ], [ 120.232889699255, 30.376884626857759 ], [ 120.23472561762912, 30.374715256402908 ], [ 120.23753893270623, 30.372831701970483 ], [ 120.23727672058169, 30.372354698010952 ], [ 120.23610835007287, 30.370042204267634 ], [ 120.23746747960104, 30.368993139672966 ], [ 120.24110326892708, 30.371360778481176 ], [ 120.24174699909065, 30.370645379771304 ], [ 120.24199606783441, 30.368052113062731 ], [ 120.24312973022461, 30.367755889892635 ], [ 120.24454593658447, 30.366253852844295 ], [ 120.24476051330566, 30.364623069763297 ], [ 120.24253599680378, 30.361807394656353 ], [ 120.23943482621701, 30.360850650013219 ], [ 120.24046307815308, 30.360505557400757 ], [ 120.24054654881184, 30.358931958871949 ], [ 120.23916542530071, 30.358291876309522 ], [ 120.23942474173975, 30.355887436853607 ], [ 120.23980615173059, 30.354862296699196 ], [ 120.23849487304699, 30.352716529487282 ], [ 120.23295997365494, 30.349512094344448 ], [ 120.22620021725754, 30.344954752909018 ], [ 120.22826642809389, 30.341758846843675 ], [ 120.22414440980367, 30.338666367897531 ], [ 120.22559859666217, 30.337021208542808 ], [ 120.22493104879686, 30.336520600685617 ], [ 120.22381052847788, 30.336782813465106 ], [ 120.22369143865944, 30.336353660022723 ], [ 120.22314298061247, 30.336091447243234 ], [ 120.22257070420903, 30.336282206262638 ], [ 120.22121179012174, 30.33618671935983 ], [ 120.2208017349767, 30.334427404377607 ], [ 120.21966211766994, 30.334184075109022 ], [ 120.21923296422756, 30.33396949838783 ], [ 120.21813626226503, 30.3331826459181 ], [ 120.22035348323698, 30.332872797080313 ], [ 120.22016272421763, 30.332371975091633 ], [ 120.21947135865048, 30.332586551812824 ], [ 120.21851777964912, 30.332682038060682 ], [ 120.21870832388231, 30.33194282164925 ], [ 120.2165865891584, 30.331823731175916 ], [ 120.21652178329532, 30.330908344840338 ], [ 120.21603105036775, 30.330448079476014 ], [ 120.21724694921431, 30.330400443417716 ], [ 120.21735423757491, 30.329089057995589 ], [ 120.21705619052227, 30.329089057995589 ], [ 120.21703956030251, 30.328366686590073 ], [ 120.21749253210146, 30.32813065088709 ], [ 120.22242779668886, 30.33087251220428 ], [ 120.21701574309191, 30.322973728179875 ], [ 120.22297561168671, 30.324041247367973 ], [ 120.22294750087894, 30.324406671497854 ], [ 120.2294266458922, 30.327447652816886 ], [ 120.22993787502855, 30.326262759645033 ], [ 120.23037314414989, 30.32634794712078 ], [ 120.23018002510082, 30.327082872390747 ], [ 120.23681044578564, 30.329641699791011 ], [ 120.23701429367065, 30.329115986823979 ], [ 120.23781895637512, 30.328590273857174 ], [ 120.23967504501354, 30.329550504684448 ], [ 120.23994326591503, 30.330011844635067 ], [ 120.23920297622681, 30.330666303634757 ], [ 120.24111270904541, 30.331406593322811 ], [ 120.24152576923382, 30.331041812896842 ], [ 120.24243235588074, 30.331851840019169 ], [ 120.24494826793671, 30.332490205764884 ], [ 120.24665951728821, 30.332677960395927 ], [ 120.2482795715332, 30.333976149559021 ], [ 120.24727642536163, 30.335724949836788 ], [ 120.25356888771068, 30.338192582130489 ], [ 120.25392293930065, 30.337446928024349 ], [ 120.25368154048931, 30.337275266647339 ], [ 120.25428235530853, 30.336449146270809 ], [ 120.25646030902863, 30.334281921386776 ], [ 120.25666415691376, 30.334185361862126 ], [ 120.25657296180736, 30.333799123764095 ], [ 120.25702357292175, 30.33257603645319 ], [ 120.2584183216095, 30.331567525863704 ], [ 120.25861144065868, 30.331090092659053 ], [ 120.25994181632996, 30.328617095947322 ], [ 120.26105761528027, 30.329486131668205 ], [ 120.26356816291809, 30.33107399940485 ], [ 120.26275277137756, 30.332361459731999 ], [ 120.26201248168945, 30.333820581436214 ], [ 120.2650809288026, 30.335515737533626 ], [ 120.26551008224499, 30.335108041763363 ], [ 120.2662181854248, 30.335440635681266 ], [ 120.26589632034302, 30.336052179336605 ], [ 120.26923298835766, 30.338187217712402 ], [ 120.26912033557892, 30.338788032531795 ], [ 120.2700001001358, 30.339061617851371 ], [ 120.26980161666881, 30.339351296424923 ], [ 120.2705579996109, 30.339726805687008 ], [ 120.27112126350403, 30.340268611908073 ], [ 120.27171671390533, 30.341067910194454 ], [ 120.27245700359356, 30.341459512710571 ], [ 120.27370154857635, 30.341781377792358 ], [ 120.27460813522339, 30.341421961784476 ], [ 120.27452766895294, 30.340794324874935 ], [ 120.27349233627319, 30.340451002121029 ], [ 120.27389466762543, 30.339447855949402 ], [ 120.2746993303299, 30.339624881744498 ], [ 120.2750426530838, 30.337817072868404 ], [ 120.27679681777965, 30.333552360534725 ], [ 120.2776873111726, 30.332817435264531 ], [ 120.27780532836914, 30.332012772560063 ], [ 120.27876555919647, 30.331556797027531 ], [ 120.27733862400066, 30.328965783119259 ], [ 120.27691530039817, 30.32850678617956 ], [ 120.27638375759125, 30.327930450439567 ], [ 120.27562737464905, 30.327522754669246 ], [ 120.27468860149384, 30.32833814620966 ], [ 120.27418971061707, 30.327490568161068 ], [ 120.27373373508453, 30.327093601226863 ], [ 120.27236044406891, 30.326321125030631 ], [ 120.27200639247894, 30.325929522514343 ], [ 120.27089059352875, 30.325398445129451 ], [ 120.26994109153748, 30.324663519859257 ], [ 120.26882529258728, 30.32392081611755 ], [ 120.268213748932, 30.322807431221122 ], [ 120.27064919471741, 30.322244167327995 ], [ 120.27493000030518, 30.321729183197078 ], [ 120.28810501098633, 30.320184230804387 ], [ 120.28969287872314, 30.317072868347111 ], [ 120.2907443046571, 30.313532352447453 ], [ 120.2906370162965, 30.309830904006901 ], [ 120.29060482978821, 30.309067976468157 ], [ 120.29124378554127, 30.308212029940478 ], [ 120.29073894023895, 30.307449102401733 ], [ 120.29070138931274, 30.305925607681331 ], [ 120.29042243957531, 30.301483869552726 ], [ 120.29027223587036, 30.298275947570801 ], [ 120.28796298376881, 30.298659605623698 ], [ 120.28653860092174, 30.298790931701717 ], [ 120.28477907180786, 30.29786825180048 ], [ 120.28306245803833, 30.297932624816951 ], [ 120.28034806251526, 30.297310352325439 ], [ 120.27812719345093, 30.297492742538395 ], [ 120.27559518814098, 30.297052860260067 ], [ 120.27446866035473, 30.296741724014225 ], [ 120.27225615976931, 30.296064627000419 ], [ 120.27130365371715, 30.295743942260856 ], [ 120.26913642883301, 30.295003652572632 ], [ 120.26757001876842, 30.294520854949951 ], [ 120.2677631378175, 30.294220447540226 ], [ 120.2676558494569, 30.29365181922924 ], [ 120.26754856109631, 30.293608903884888 ], [ 120.26722669601452, 30.294241905212516 ], [ 120.26685118675243, 30.294102430343685 ], [ 120.2668297290802, 30.293748378753719 ], [ 120.26611089706421, 30.2937269210816 ], [ 120.26553153991711, 30.29355525970459 ], [ 120.26544570922852, 30.29338359832775 ], [ 120.26481270790111, 30.293458700180111 ], [ 120.25563225688541, 30.289740732187113 ], [ 120.25135037802113, 30.287779605205401 ], [ 120.24866816900624, 30.285979521431784 ], [ 120.24631747981414, 30.28475654053176 ], [ 120.2421914927254, 30.281139528960978 ], [ 120.23921027175686, 30.279424309337571 ], [ 120.23482335815061, 30.275979172947757 ], [ 120.23345232009899, 30.274136066436768 ], [ 120.23183226585388, 30.27223706245428 ], [ 120.23071539166267, 30.270369172685719 ], [ 120.22240870101098, 30.258631611577584 ], [ 120.21878235245833, 30.25287859476532 ], [ 120.21397583390353, 30.246076298244589 ], [ 120.21124362945568, 30.242261767780292 ], [ 120.20535478586692, 30.235347676643869 ], [ 120.19451200962078, 30.244277715682927 ], [ 120.18669605255127, 30.248301029205265 ], [ 120.18440544605255, 30.248252749443054 ], [ 120.18149256706238, 30.248923301696777 ], [ 120.18348276615154, 30.260193943977413 ], [ 120.18522620201122, 30.273653268814087 ], [ 120.18164277076721, 30.280723571777457 ], [ 120.17936289310455, 30.28549253940588 ], [ 120.18286585807812, 30.299450755119267 ], [ 120.1852047443391, 30.2999764680863 ], [ 120.18578410148621, 30.299445390701408 ], [ 120.18650829792023, 30.299445390701408 ], [ 120.18649220466625, 30.299997925758419 ], [ 120.18640637397777, 30.300598740577811 ], [ 120.18701255321503, 30.301494598388672 ], [ 120.18960893154144, 30.300813317299003 ], [ 120.18963575363159, 30.301108360290641 ], [ 120.18854141235352, 30.305142402648926 ], [ 120.18755435943604, 30.305120944976863 ], [ 120.18682479858398, 30.304520130157414 ], [ 120.18315553665161, 30.304327011108512 ], [ 120.18328428268433, 30.312180519104061 ], [ 120.18444299697876, 30.312137603759709 ], [ 120.18517255783092, 30.315248966216984 ], [ 120.19021511077892, 30.314369201660156 ], [ 120.1908802986145, 30.316354036331177 ], [ 120.19002199172974, 30.316997766494751 ], [ 120.19125580787659, 30.317416191101188 ], [ 120.19472122192394, 30.31685829162592 ], [ 120.19657731056225, 30.316461324691772 ], [ 120.20296096801769, 30.322458744049186 ], [ 120.20234942436218, 30.322726964950675 ], [ 120.20134091377258, 30.322726964950675 ], [ 120.19973158836365, 30.322855710983333 ], [ 120.19785404205322, 30.322780609130973 ], [ 120.19683480262756, 30.322651863098088 ], [ 120.19553821534669, 30.322671215147125 ], [ 120.1939594745636, 30.32269477844244 ], [ 120.19227504730225, 30.322480201721248 ], [ 120.192049741745, 30.324347019195557 ], [ 120.19774675369263, 30.324486494064388 ], [ 120.19871234893799, 30.326428413391227 ], [ 120.20007501818338, 30.328376234101654 ], [ 120.19940736292483, 30.328650463125143 ], [ 120.1985693281988, 30.328684684243171 ], [ 120.19765491488215, 30.328722024278193 ], [ 120.19828673586755, 30.333180391808128 ], [ 120.20000334963709, 30.33301355837466 ], [ 120.20007491111755, 30.333907592303149 ], [ 120.19953846931458, 30.333943319346872 ], [ 120.19955037816555, 30.334563231481297 ], [ 120.20042059390107, 30.334610974810062 ], [ 120.20742416381836, 30.334453582763615 ], [ 120.20722278304902, 30.347723972136066 ], [ 120.20358796122309, 30.347846710537908 ], [ 120.20208120346069, 30.348026847787025 ], [ 120.20057916641235, 30.347981357626963 ], [ 120.19970648390881, 30.348367810511206 ], [ 120.19865741800459, 30.350132060339035 ], [ 120.19710753076652, 30.350274968513929 ], [ 120.19553403963073, 30.352373099667545 ], [ 120.19937260258303, 30.354089713437077 ], [ 120.2044748071786, 30.356903029169302 ], [ 120.20386777076067, 30.358400340919616 ], [ 120.20744390581967, 30.360927624860835 ], [ 120.20892233995266, 30.361118169094027 ], [ 120.20944676420186, 30.361785931745601 ], [ 120.20968494383862, 30.362739510746962 ], [ 120.21115897910931, 30.364427590357138 ], [ 120.20684716702829, 30.366659188257529 ], [ 120.20669696365076, 30.367989563765263 ], [ 120.20933389532729, 30.369546314832519 ], [ 120.21007289760746, 30.370619198438476 ], [ 120.20945298580045, 30.371859022707326 ], [ 120.20987516638354, 30.377339100523386 ], [ 120.21132710011443, 30.378045916622909 ], [ 120.21150820291962, 30.37863750447292 ], [ 120.21349647024192, 30.381026171380711 ], [ 120.22062556734465, 30.382889449465438 ], [ 120.22240172635065, 30.384165000778012 ], [ 120.22366397417511, 30.383861052996565 ], [ 120.22532705050264, 30.384287738852493 ], [ 120.22599234573147, 30.384749185950625 ], [ 120.22616153935826, 30.386197578736756 ], [ 120.2258026599884, 30.386703014373722 ], [ 120.22604942338148, 30.388761770555107 ], [ 120.22686245407385, 30.390053951759967 ], [ 120.22825763429864, 30.390882537390155 ], [ 120.23581545625348, 30.387234733170715 ] ] ] } }
]
}

geojsonlayer.js
( Esri/geojson-layer-js

define([
  "dojo/_base/declare",
  "esri/graphic",
  "esri/layers/GraphicsLayer",
  "esri/InfoTemplate",
  "esri/graphicsUtils",
  "esri/Color",
  "esri/symbols/SimpleMarkerSymbol",
  "esri/symbols/SimpleLineSymbol",
  "esri/symbols/SimpleFillSymbol",
  "esri/renderers/SimpleRenderer",
  "esri/SpatialReference",
  "esri/geometry/webMercatorUtils",
  "esri/request",
  "esri/config",
  "dojo/_base/url",
  "dojo/_base/lang"
],  function (declare, Graphic, GraphicsLayer, InfoTemplate, graphicsUtils, Color, SimpleMarkerSymbol,
              SimpleLineSymbol, SimpleFillSymbol, SimpleRenderer, SpatialReference, webMercatorUtils, esriRequest, esriConfig, Url, lang
) {
  return declare([GraphicsLayer], {

    // Required Terrformer library reference
    _terrafomer: (typeof Terraformer !== 'undefined') ? Terraformer : null,

    constructor: function (options) {
      // options:
      //      url: String
      //          Path to file or server endpoint. Server must be on the same domain or cors enabled. Or use a proxy.
      //      data: Object[]
      //          Optional: FeatureCollection of GeoJson features. This will override url if both are provided.
      //      maxdraw: Number
      //          Optional: The maximum graphics to render. Default is 1000.
      this._validState = true;
      // First look for url
      this._url = options.url;
      // Accept data as geojson features array. This will override options.url!
      this._data = options.data;
      // GeoJSON spatial reference (not optional)
      this._inSpatialReference = new SpatialReference({wkid: 4326});  // Data must be in Geographic Coordinates
      // GeoJSON transformation (optional)
      this._outSpatialReference = null;
      // Default popup
      if (options.infoTemplate !== false) {
        this.setInfoTemplate(options.infoTemplate || new InfoTemplate("GeoJSON Data", "${*}"));
      }
      // Renderer
      if (options.renderer) {
        this.renderer = options.renderer;
      }
      // Default symbols
      this._setDefaultSymbols();
      // Enable browser to make cross-domain requests
      this._setCorsSevers();
      this._setXhrDefaults(10000);
      // Manage drawing
      this._maxDraw = options.maxdraw || 1000;  // default to 1000 graphics
      this._drawCount = 0;
      this._drawCountTotal = 0;
      // Extended public properties
      this.extent = null;
      // Graphics layer does not call onLoad natively but we'll call it after features have been added to layer
      if (options.onLoad && typeof options.onLoad === 'function') {
        this.onLoad = options.onLoad;
      }
      // Make sure the environment is good to go!
      this._updateState();
    },

    _setDefaultSymbols: function () {
      function getRandomColor(mainColor, transparency) {
        function getRandomInt(min, max) {
          return Math.floor(Math.random() * (max - min + 1)) + min;
        }
        switch (mainColor) {
          case "red":
            return new Color([getRandomInt(150, 255), getRandomInt(0, 255), getRandomInt(0, 255), transparency]);
          case "green":
            return new Color([getRandomInt(0, 155), getRandomInt(150, 255), getRandomInt(0, 155), transparency]);
          case "blue":
            return new Color([getRandomInt(0, 255), getRandomInt(0, 255), getRandomInt(150, 255), transparency]);
        }
      }
      // Random colors
      // this._simplePointSym = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 8,
      //   new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, getRandomColor("blue", 0.5), 1),
      //   getRandomColor("blue", 0.75));
      // this._simpleLineSym = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, getRandomColor("red", 0.9), 2);
      // this._simplePolygonSym = new SimpleFillSymbol("solid",
      //   new SimpleLineSymbol("solid", new Color([50, 50, 50, 0.15]), 1),
      //   getRandomColor("green", 0.15));
      // Option to hardcod colors here
      this._simplePointSym = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 8,
           new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([150, 150, 150]), 0.5),
           new Color([50, 130, 255, 0.75]));
      this._simpleLineSym = new SimpleLineSymbol("solid", new Color([255, 50, 50, 0.75]), 2);
      this._simplePolygonSym = new SimpleFillSymbol("solid",
              new SimpleLineSymbol("solid", new Color([0, 0, 0, 0.10]), 1),
              new Color([0, 0, 0, 0.30]));
    },

    _setCorsSevers: function () {
      // Allow browser to make cross-domain requests
      esriConfig.defaults.io.corsEnabledServers.push("http://sampleserver6.arcgisonline.com");
      // Add server
      if (this._url) {
        var url = new Url(this._url),
          scheme = url.scheme,
          host = url.host,
          port = url.port,
          server = scheme + "://" + host; // + "/"; // + (port ? port : ""); // TODO
        if (scheme && host) {
          esriConfig.defaults.io.corsEnabledServers.push(server);
        }
      }
    },

    _setXhrDefaults: function (postSize) {
      esriConfig.defaults.io.postLength = postSize;  // max size of post request
    },

    _updateState: function () {
      if (!this._inSpatialReference) {
        this._validState = false;
        console.error("GeoJsonLayer Error: Invalid SpatialReference.");
      }
      if (!this._terrafomer) {
        this._validState = false;
        console.error("GeoJsonLayer Error: Invalid Terraformer reference. Please add a reference to your html page.");
      }
    },

    // GraphicLayer overrides
    _setMap: function (map, surface) {
      var div = this.inherited(arguments);
      // Check spatial reference
      if (!this._validState) {
        return div;
      }
      // Set sr if need to project
      this._outSpatialReference = map.spatialReference;
      // Load GeoJSON as graphics
      this._loadGeoJson();
      // Return div - sets this.id
      return div;
    },

    _unsetMap: function () {
      return this.inherited(arguments);
    },

    add: function (graphic) {
      if (!this._validState) {
        return;
      }
      // Can do more with graphic if needed
      this.inherited(arguments);
      this._incrementDrawCount();
      return;
    },

    clear: function () {
      this._drawCount = 0;
      this._drawCountMax = 0;
      return this.inherited(arguments);
    },

    // GeoJsonLayer class functions
    _loadGeoJson: function () {
      if (this._data) {
        // Load data
        this._getGeoJson(this._data);
      } else if (this._url) {
        // XHR request
        this._getGeoJsonXhr(this._url);
      }
    },

    _getGeoJsonXhr: function (url) {
      // xhr request to get data
      var requestHandle = esriRequest({
        url: url,
        handleAs: "json"
      });
      requestHandle.then(lang.hitch(this, this._getGeoJson),
        lang.hitch(this, this._errorGetGeoJsonXhr));
    },

    _getGeoJson: function (geojson) {
      // Check data
      if (geojson.type !== "FeatureCollection" || !geojson.features) {
        console.error("GeoJsonLayer Error: Invalid GeoJSON FeatureCollection. Check url or data structure.");
        return;
      }
      // Convert GeoJSON to ArcGIS JSON
      var arcgisJson = this._terraformerConverter(geojson);
      // Add graphics to layer
      this._addGraphics(arcgisJson);
    },

    // GeoJSON to ArcGIS JSON
    _terraformerConverter: function (geojson) {
      var json,
        arcgis;
      // Convert the geojson object to a Primitive json representation
      json = new this._terrafomer.Primitive(geojson);
      // Convert to ArcGIS JSON
      arcgis = this._terrafomer.ArcGIS.convert(json);
      return arcgis;
    },

    _errorGetGeoJsonXhr: function (e) {
      console.error("GeoJsonLayer Error: Couldn't load GeoJSON. Check url. File must be on the same domain or server must be CORS enabled.\n\n" + e);
    },

    _incrementDrawCount: function () {
      this._drawCount++;
      if (this._drawCount === this._drawCountTotal) {
        this._updateLayerExtent();
        this.onUpdateEnd();
      }
    },

    _decrementDrawCount: function () {
      this._drawCountTotal--;
    },

    _updateLayerExtent: function () {
      var extent;
      if (this.graphics.length) {
        extent = graphicsUtils.graphicsExtent(this.graphics);
      }
      this.extent = extent;
    },

    // Type functions
    _getEsriSymbol: function (geometryType) {
      var sym;
      switch (geometryType) {
        case "point":
          sym = this._simplePointSym;
          break;
        case "multipoint":
          sym = this._simplePointSym;
          break;
        case "polyline":
          sym = this._simpleLineSym;
          break;
        case "polygon":
          sym = this._simplePolygonSym;
          break;
        case "extent":
          sym = this._simplePolygonSym;
          break;
      }
      return sym;
    },

    _addGraphicToLayer: function (graphic) {
      // Add or project and then add graphic
      if (this._inSpatialReference.wkid === 4326 || this._inSpatialReference.wkid === 102100) {
        // ArcGIS API automatically translates between these!
        // if (graphic.geometry.spatialReference.wkid === 4326) {
        //     graphic.setGeometry(webMercatorUtils.geographicToWebMercator(graphic.geometry));
        // }
        // Add graphic directly
        this.add(graphic);
      }
    },

    _createGraphic: function (arcgisJson) {
      var graphic;
      // This magically sets geometry type!
      graphic = new Graphic(arcgisJson);
      // Set the correct symbol based on type and render - NOTE: Only supports simple renderers
      if (this.renderer && this.renderer.symbol) {
        //graphic.setSymbol(this.render.getSymbol(graphic));  // use for complex renderers
        graphic.setSymbol(this.renderer.symbol);
      } else {
        graphic.setSymbol(this._getEsriSymbol(graphic.geometry.type));
      }
      // Update SR because terraformer sets incorrect spatial reference
      graphic.geometry.setSpatialReference(this._inSpatialReference); // NOTE: Has to match features!
      return graphic;
    },

    _addGraphics: function (arcgisJson) {
      var i,
        feature,
        graphic;
      // Limit size of data that can be drawn
      if (arcgisJson.length > this._maxDraw) {
        this._drawCountTotal = this._maxDraw;
        console.warn("GeoJsonLayer Warning: Large dataset detected. Only drawing the first " + this._maxDraw + " features!");
      } else {
        this._drawCountTotal = arcgisJson.length;
      }
      // Add graphics to the layer with symbols, project if needed
      for (i = 0; i < this._drawCountTotal; i++) {
        feature = arcgisJson[i];
        // Create graphic - magically sets the geometry type!
        graphic = this._createGraphic(feature);
        // Add to layer
        this._addGraphicToLayer(graphic);
      }
      // Call onLoad
      // emit mixes in `layer` and `target` properties as event object
      // onLoad provided via constructor just returns `layer` object
      this.onLoad(this);
    }
  });
});

更多参考:

https://github.com/Esri/Terraformer

Arcgis for js,Openlayers中加载GeoJSON

上一篇下一篇

猜你喜欢

热点阅读