arcgis for js4.x自定义Graphic数组创建FeatureLayer添加标注

发布时间 2023-09-19 18:10:26作者: ZerlinM
                            var point = [{
								"geometry": {
									"x": 116.820688,
									"y": 33.974053,
									"spatialReference": {
										"wkid": 4326
									}
								},
								"attributes": {
									"cxcd": "监测站",
									"name": "相山区管网监测站",
									"pop": "4000",
									"x": "116.820688",
									"y": "33.974053"
								},
							}, {
								"geometry": {
									"x": 116.855988,
									"y": 34.064053,
									"spatialReference": {
										"wkid": 4326
									}
								},
								"attributes": {
									"cxcd": "监测站",
									"name": "杜集区管网监测站",
									"pop": "4000",
									"x": "116.855988",
									"y": "34.064053"
								},
							}, {
								"geometry": {
									"x": 116.850688,
									"y": 33.854053,
									"spatialReference": {
										"wkid": 4326
									}
								},
								"attributes": {
									"cxcd": "监测站",
									"name": "烈山区管网监测站",
									"pop": "4000",
									"x": "116.850688",
									"y": "33.854053"
								},
							}, {
								"geometry": {
									"x": 116.600688,
									"y": 33.704053,
									"spatialReference": {
										"wkid": 4326
									}
								},
								"attributes": {
									"cxcd": "监测站",
									"name": "濉溪县管网监测站#1",
									"pop": "4000",
									"x": "116.600688",
									"y": "33.704053"
								},
							}, {
								"geometry": {
									"x": 116.700688,
									"y": 33.90053,
									"spatialReference": {
										"wkid": 4326
									}
								},
								"attributes": {
									"cxcd": "监测站",
									"name": "濉溪县管网监测站#3",
									"pop": "4000",
									"x": "116.700688",
									"y": "33.90053"
								},
							}, {
								"geometry": {
									"x": 116.800688,
									"y": 33.604053,
									"spatialReference": {
										"wkid": 4326
									}
								},
								"attributes": {
									"cxcd": "监测站",
									"name": "濉溪县管网监测站#2",
									"pop": "4000",
									"x": "116.800688",
									"y": "33.604053"
								},
							}];
							var gras = [];
							for (var i = 0; i < point.length; i++) {
								gras.push(new Graphic({
									geometry: new Point({
										longitude: point[i].geometry.x,
										latitude: point[i].geometry.y
									}),
									attributes: point[i].attributes
								}))
							}
							var Symbol1 = {
								type: "picture-marker",
								url: 'img/fz.gif', //图片地址
								width: "110px",
								height: "110px",
							};
							var renderer = {
								type: "class-breaks",
								field: "pop",
								classBreakInfos: [{
									minValue: 4000,
									maxValue: 7000,
									symbol: Symbol1
								}]
							};
							var fields = [{
								name: "ObjectID",
								alias: "ObjectID",
								type: "oid",
							}];
 
							for (var col in gras[0]["attributes"]) {
								fields.push({
									name: col,
									alias: col,
									type: "string"
								})
							}
                            //标注
                            const nameClass0 = {
								labelPlacement: "center-along",
								labelExpressionInfo: {
									expression: "$feature.name"
								},
							};
 
							var layerzz = new FeatureLayer({
								source: gras,
								renderer: renderer,
								geometryType: "point",
								fields: fields,
								objectIdField: "ObjectID",
								labelingInfo: [
								    nameClass0
								],
							});
							map.add(layerzz);