最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get JSON WebMap from ArcGIS JavaScript API Map object - Stack Overflow

programmeradmin1浏览0评论

I'm trying to get a WebMap object (as JSON) from a JavaScript Map object in the ArcGIS JavaScript API. Is there any way to do this within the API, without using ArcGIS? Ideally something like:

webMapAsJSON = map.toWebMap();

From the "Export Web Map Task" documentation in the REST API, there's this line that suggests it should exist:

"The ArcGIS web APIs (for JavaScript, Flex, Silverlight, etc.) allow developers to easily get this JSON string from the map."

However, I don't see anything in the Map object or elsewhere in the API that would do this.

I'm trying to get a WebMap object (as JSON) from a JavaScript Map object in the ArcGIS JavaScript API. Is there any way to do this within the API, without using ArcGIS.? Ideally something like:

webMapAsJSON = map.toWebMap();

From the "Export Web Map Task" documentation in the REST API, there's this line that suggests it should exist:

"The ArcGIS web APIs (for JavaScript, Flex, Silverlight, etc.) allow developers to easily get this JSON string from the map."

However, I don't see anything in the Map object or elsewhere in the API that would do this.

Share Improve this question edited Dec 18, 2015 at 15:02 Erica 2,4875 gold badges29 silver badges34 bronze badges asked Dec 17, 2015 at 23:39 pdpcpdpc 1313 silver badges7 bronze badges 1
  • Cross-posted to GIS.SE: gis.stackexchange./questions/174356/… – Erica Commented Dec 18, 2015 at 14:23
Add a ment  | 

3 Answers 3

Reset to default 3

You can't. At least not officially. The steps outlined below are not remended. They use part of the ArcGIS JS library that is not part of the public API and therefore this behavior may not work in the next version of the API or they may back-patch a previous version of the API and this could stop working even on something that previously did work.

That said, sometimes you need some "future" functionality right now and this is actually a pretty straightforward way of getting what you want using the mon proxy pattern

Use the undocumented "private" function _getPrintDefinition

var proxy_getPrintDefinition = printTask._getPrintDefinition;

printTask._getPrintDefinition = function() {
    var getPrintDefResult = proxy_getPrintDefinition.apply(this, arguments);
    //Now you can do what you want with getPrintDefResults
    //which should contain the Web_Map_as_JSON

    console.log(Json.stringify(getPrintDefResult));

    //make sure you return the result or you'll break this print task.
    return getPrintDefResult;
    }

_getPrintDefinition takes the map as the first argument and a PrintParameters object as the second.

so you'll have to create a PrintTask, redefine the _getPrintDefinition function on the newly created print task as outlined above, create a PrintParameters and then run:

myPrintTask._getPrintDefinition(myMap,myPrintParameters);

The results of this on my little test are:

{"mapOptions":{"showAttribution":false,"extent":{"xmin":-7967955.990468411,"ymin":5162705.099750506,"xmax":-7931266.216891576,"ymax":5184470.54355468,
"spatialReference":{"wkid":102100,"latestWkid":3857}},"spatialReference":{"wkid":102100,"latestWkid":3857}},
"operationalLayers":[
    {"id":"layer0","title":"layer0","opacity":1,"minScale":591657527.591555,"maxScale":70.5310735,"url":"http://services.arcgisonline./ArcGIS/rest/services/World_Street_Map/MapServer"},
    {"id":"XXX-Redacted-XXX","title":"serviceTitle","opacity":1,"minScale":0,"maxScale":0,"token":"XXX-Redacted-XXX","url":"http://XXX-Redacted-XXX/arcgis/rest/services/TestService/MapServer"},
    {"id":"XXX-Redacted-XXX","opacity":1,"minScale":0,"maxScale":0,"featureCollection":{"layers":[]}},
    {"id":"featureGraphics","opacity":1,"minScale":0,"maxScale":0,"featureCollection":{"layers":[]}},
    {"id":"map_graphics","opacity":1,"minScale":0,"maxScale":0,"featureCollection":{"layers":[]}}
]}

if you don't need to do any operations on the web map json and just need the output then you don't even need to use the proxy pattern.

@Suttikeat Witchayakul's answer above should work if your goal is to print the map using a print service.

However, if you are trying to export the map to the web map JSON spec so that you can save it to ArcGIS Online/Portal, or re-instantiate a map object from it later, you may have some problems. This is because the web map specification is not the same as the export web map specification, which what the print task generates and sends to printing services.

Unfortunately, the ArcGIS API for JavaScript does not provide any methods to export a map object to web map JSON. This is supposed to be ing in version 4... at some point. Until then, you can use the all but abandoned cereal library. However, if your map uses layer types that are not fully supported by cereal, it may not work for you as is and you would have to extend it.

If you want to use "esri/tasks/PrintTask" to export your map, you must use "esri/tasks/PrintParameters" for execute the printTask. Just set your map object directly to printParameter.

require([
  "esri/map", "esri/tasks/PrintTemplate", "esri/tasks/PrintParameters", ... 
], function(Map, PrintTemplate, PrintParameters, ... ) {
  var map = new Map( ... );

  var template = new PrintTemplate();
  template.exportOptions = {
    width: 500,
    height: 400,
    dpi: 96
  };
  template.format = "PDF";
  template.layout = "MAP_ONLY";
  template.preserveScale = false;

  var params = new PrintParameters();
  params.map = map;
  params.template = template;
  printTask.execute(params, printResult);
});
发布评论

评论列表(0)

  1. 暂无评论