|
17 | 17 | restrict: 'E', |
18 | 18 | scope: { |
19 | 19 | geometry: '=', |
| 20 | + geometries: '=', |
20 | 21 | baseMap: '=', |
21 | 22 | zoom: '=', |
22 | 23 | mapId: '=' |
|
39 | 40 | ctrl.mapZoom = $scope.zoom ? $scope.zoom : 6; |
40 | 41 | ctrl.mapGeometry = null; |
41 | 42 |
|
42 | | - // Generate objects based on the provided geometry |
43 | | - if ($scope.geometry && angular.isArray($scope.geometry)) { |
44 | | - // Use multi-point when an array-of-arrays. |
45 | | - if (angular.isArray($scope.geometry[0])) { |
46 | | - for (i=0; i < $scope.geometry.length; i++) { |
47 | | - if (!isNaN($scope.geometry[i][0]) && $scope.geometry[i].length === 2) { |
48 | | - geometryData.push({ |
49 | | - 'type': 'point', |
50 | | - 'latitude': $scope.geometry[i][0], |
51 | | - 'longitude': $scope.geometry[i][1] |
52 | | - }); |
53 | | - } |
54 | | - } |
55 | | - } else if (!isNaN($scope.geometry[0]) && $scope.geometry.length === 2) { |
56 | | - geometryData.push({ |
57 | | - 'type': 'point', |
58 | | - 'latitude': $scope.geometry[0], |
59 | | - 'longitude': $scope.geometry[1] |
60 | | - }); |
| 43 | + if ($scope.geometry) { |
| 44 | + geometryData = processGeometry($scope.geometry); |
| 45 | + } |
61 | 46 |
|
62 | | - } |
| 47 | + if ($scope.geometries) { |
| 48 | + geometryData = processGeoJSON($scope.geometries); |
63 | 49 | } |
64 | 50 |
|
65 | 51 | // Have to wait for scope value of <mapId> to be updated in view |
|
70 | 56 | }, 300); |
71 | 57 | }); |
72 | 58 |
|
| 59 | + /** |
| 60 | + * Processes the <geometry> scope variable which can contain point data. |
| 61 | + */ |
| 62 | + function processGeometry(geometry) { |
| 63 | + var geometryData = []; |
| 64 | + |
| 65 | + // Generate objects based on the provided geometry |
| 66 | + if (geometry && angular.isArray(geometry)) { |
| 67 | + // Use multi-point when an array-of-arrays. |
| 68 | + if (angular.isArray(geometry[0])) { |
| 69 | + for (i=0; i < geometry.length; i++) { |
| 70 | + if (!isNaN(geometry[i][0]) && geometry[i].length === 2) { |
| 71 | + geometryData.push({ |
| 72 | + 'type': 'point', |
| 73 | + 'latitude': geometry[i][0], |
| 74 | + 'longitude': geometry[i][1] |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + else if (!isNaN(geometry[0]) && geometry.length === 2) { |
| 80 | + geometryData.push({ |
| 81 | + 'type': 'point', |
| 82 | + 'latitude': geometry[0], |
| 83 | + 'longitude': geometry[1] |
| 84 | + }); |
| 85 | + |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return geometryData; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Processes the <geometry> scope variable which can contain point data. |
| 94 | + */ |
| 95 | + function processGeoJSON(geoJsonData) { |
| 96 | + var geometryData = []; |
| 97 | + var convertedData = null; |
| 98 | + |
| 99 | + // Generate objects based on the provided geometry |
| 100 | + if (geoJsonData) { |
| 101 | + if (angular.isArray(geoJsonData)) { |
| 102 | + for (var i=0; i < geoJsonData.length; i++) { |
| 103 | + convertedData = convertSingleGeoJSON(geoJsonData[i]); |
| 104 | + if (convertedData) { |
| 105 | + if (angular.isArray(convertedData)) { |
| 106 | + geometryData = geometryData.concat(convertedData); |
| 107 | + } |
| 108 | + else { |
| 109 | + geometryData.push(convertedData); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + else { |
| 115 | + convertedData = convertSingleGeoJSON(geoJsonData); |
| 116 | + if (convertedData) { |
| 117 | + if (angular.isArray(convertedData)) { |
| 118 | + geometryData = geometryData.concat(convertedData); |
| 119 | + } |
| 120 | + else { |
| 121 | + geometryData.push(convertedData); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return geometryData; |
| 128 | + } |
| 129 | + |
| 130 | + function convertSingleGeoJSON(geoJsonData) { |
| 131 | + var geometryData = null; |
| 132 | + |
| 133 | + if (geoJsonData && geoJsonData.type) { |
| 134 | + if (geoJsonData.type === 'Point') { |
| 135 | + geometryData = { |
| 136 | + 'type': 'point', |
| 137 | + 'latitude': geoJsonData.coordinates[1], |
| 138 | + 'longitude': geoJsonData.coordinates[0] |
| 139 | + }; |
| 140 | + } |
| 141 | + else if (geoJsonData.type === 'MultiPoint') { |
| 142 | + geometryData = { |
| 143 | + 'type': 'multipoint', |
| 144 | + 'points': geoJsonData.coordinates |
| 145 | + }; |
| 146 | + } |
| 147 | + else if (geoJsonData.type === 'LineString') { |
| 148 | + geometryData = { |
| 149 | + 'type': 'polyline', |
| 150 | + 'paths': [ geoJsonData.coordinates ] |
| 151 | + }; |
| 152 | + } |
| 153 | + else if (geoJsonData.type === 'MultiLineString') { |
| 154 | + geometryData = { |
| 155 | + 'type': 'polyline', |
| 156 | + 'paths': geoJsonData.coordinates |
| 157 | + }; |
| 158 | + } |
| 159 | + else if (geoJsonData.type === 'Polygon') { |
| 160 | + geometryData = { |
| 161 | + 'type': 'polygon', |
| 162 | + 'rings': geoJsonData.coordinates |
| 163 | + }; |
| 164 | + } |
| 165 | + else if (geoJsonData.type === 'MultiPolygon') { |
| 166 | + geometryData = []; |
| 167 | + for (var i=0; i < geoJsonData.coordinates.length; i++) { |
| 168 | + geometryData.push( |
| 169 | + { |
| 170 | + 'type': 'polygon', |
| 171 | + 'rings': geoJsonData.coordinates[i] |
| 172 | + } |
| 173 | + ); |
| 174 | + } |
| 175 | + } |
| 176 | + else { |
| 177 | + console.log('Unknown GeoJSON geometry type: ', geoJsonData.type); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + return geometryData; |
| 182 | + } |
| 183 | + |
73 | 184 | /** |
74 | 185 | * Initializes the map with a single feature layer for locations. |
75 | 186 | */ |
76 | 187 | function initMap(geoData) { |
77 | 188 | require([ |
78 | 189 | 'esri/map', 'esri/graphic', 'esri/symbols/SimpleFillSymbol', |
79 | | - 'esri/symbols/SimpleMarkerSymbol', 'esri/layers/GraphicsLayer', |
| 190 | + 'esri/symbols/SimpleMarkerSymbol', 'esri/symbols/SimpleLineSymbol', |
| 191 | + 'esri/layers/GraphicsLayer', |
80 | 192 | 'esri/geometry/Point', 'esri/geometry/Polygon', |
| 193 | + 'esri/geometry/Multipoint', 'esri/geometry/Polyline', |
81 | 194 | 'esri/graphicsUtils', 'esri/Color' |
82 | 195 | ], function( |
83 | 196 | Map, Graphic, SimpleFillSymbol, |
84 | | - SimpleMarkerSymbol, GraphicsLayer, |
| 197 | + SimpleMarkerSymbol, SimpleLineSymbol, |
| 198 | + GraphicsLayer, |
85 | 199 | Point, Polygon, |
| 200 | + Multipoint, Polyline, |
86 | 201 | graphicsUtils, Color |
87 | 202 | ) |
88 | 203 | { |
|
115 | 230 | for (var i=0; i < geoData.length; i++) { |
116 | 231 | if (geoData[i].type === 'point') { |
117 | 232 | ctrl.mapGeometry.push( new Point(geoData[i]) ); |
118 | | - } else if (geoData[i].type === 'polygon') { |
| 233 | + } |
| 234 | + else if (geoData[i].type === 'multipoint') { |
| 235 | + ctrl.mapGeometry.push( new Multipoint(geoData[i]) ); |
| 236 | + } |
| 237 | + else if (geoData[i].type === 'polygon') { |
119 | 238 | ctrl.mapGeometry.push( new Polygon(geoData[i]) ); |
120 | 239 | } |
| 240 | + else if (geoData[i].type === 'polyline') { |
| 241 | + ctrl.mapGeometry.push( new Polyline(geoData[i]) ); |
| 242 | + } |
121 | 243 | } |
122 | 244 | } |
123 | 245 | } |
|
129 | 251 | if (geometry && geometry.length > 0) { |
130 | 252 | for (var i=0; i < geometry.length; i++) { |
131 | 253 | var symbol = null; |
132 | | - if (geometry[i] instanceof Point) { |
| 254 | + if (geometry[i].type === 'point') { |
| 255 | + symbol = new SimpleMarkerSymbol().setColor(new Color([0, 0, 0, 0.60])); |
| 256 | + } |
| 257 | + else if (geometry[i].type === 'multipoint') { |
133 | 258 | symbol = new SimpleMarkerSymbol().setColor(new Color([0, 0, 0, 0.60])); |
134 | | - } else if (geometry[i] instanceof Polygon) { |
| 259 | + } |
| 260 | + else if (geometry[i].type === 'polygon') { |
135 | 261 | symbol = new SimpleFillSymbol().setColor( new Color([0, 0, 0, 0.60]) ); |
136 | 262 | } |
| 263 | + else if (geometry[i].type === 'polyline') { |
| 264 | + symbol = new SimpleLineSymbol().setColor( new Color([0, 0, 0, 0.60]) ); |
| 265 | + } |
137 | 266 |
|
138 | 267 | if (symbol) { |
139 | 268 | var graphic = new Graphic(geometry[i], symbol); |
|
0 commit comments