我想从文件夹中将 shapefile 添加到地图中。我使用了 JS 示例代码,但它对我不起作用。它在响应中给出错误,featurecollection。它没有进入请求块,所以我将其更改为 esri 请求,但代码仍然无法正常工作
var portalUrl = "https://www.arcgis.com";
esriConfig.defaults.io.proxyUrl = "/proxy/";
on(dojo.byId("uploadForm"), "change", function (event) {
var fileName = event.target.value.toLowerCase();
<!-- if (sniff("ie")) { //filename is full path in IE so extract the file name -->
<!-- var arr = fileName.split("\\"); -->
<!-- fileName = arr[arr.length - 1]; -->
<!-- } -->
if (fileName.indexOf(".zip") !== -1) {//is file a zip - if not notify user
generateFeatureCollection(fileName);
}
else {
alert("Add shapefile as .zip file");
}
});
function generateFeatureCollection (fileName) {
var name = fileName.split(".");
name = name[0].replace("c:\\fakepath\\", "");
var params = {
'name': name,
'targetSR': map.spatialReference,
'maxRecordCount': 1000,
'enforceInputFileSizeLimit': true,
'enforceOutputJsonSizeLimit': true
};
//var extent = scaleUtils.getExtentForScale(map, 40000);
var extent = esri.geometry.getExtentForScale(map, 40000);
var resolution = extent.getWidth() / map.width;
params.generalize = true;
params.maxAllowableOffset = resolution;
params.reducePrecision = true;
params.numberOfDigitsAfterDecimal = 0;
var myContent = {
'filetype': 'shapefile',
'publishParameters': JSON.stringify(params),
'f': 'json',
//'callback.html': 'textarea'
};
esriRequest({
url: portalUrl + '/sharing/rest/content/features/generate',
content: myContent,
form: dojo.byId('uploadForm'),
handleAs: 'json',
load: lang.hitch(this, function (response) {
<!-- if (response.error) { -->
<!-- errorHandler(response.error); -->
<!-- return; -->
<!-- } -->
var layerName = response.FeatureCollection.layers[0].layerDefinition.name;
addShapefileToMap(response.FeatureCollection);
}),
//error: lang.hitch(this, errorHandler)
});
}
function addShapefileToMap (featureCollection) {
var fullExtent;
var layers = [];
arrayUtils.forEach(featureCollection.layers, function (layer) {
var infoTemplate = new InfoTemplate("Details", "${*}");
var featureLayer = new FeatureLayer(layer, {
infoTemplate: infoTemplate
});
featureLayer.on('click', function (event) {
map.infoWindow.setFeatures([event.graphic]);
});
changeRenderer(featureLayer);
fullExtent = fullExtent ?
fullExtent.union(featureLayer.fullExtent) : featureLayer.fullExtent;
layers.push(featureLayer);
});
map.addLayers(layers);
map.setExtent(fullExtent.expand(1.25), true);
}
function changeRenderer (layer) {
//change the default symbol for the feature collection for polygons and points
var symbol = null;
switch (layer.geometryType) {
case 'esriGeometryPoint':
symbol = new PictureMarkerSymbol({
'angle': 0,
'xoffset': 0,
'yoffset': 0,
'type': 'esriPMS',
'url': 'https://static.arcgis.com/images/Symbols/Shapes/BluePin1LargeB.png',
'contentType': 'image/png',
'width': 20,
'height': 20
});
break;
case 'esriGeometryPolygon':
symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
new Color([112, 112, 112]), 1), new Color([136, 136, 136, 0.25]));
break;
}
if (symbol) {
layer.setRenderer(new SimpleRenderer(symbol));
}
}