Skip to content

Instantly share code, notes, and snippets.

@nommuna2
Last active April 26, 2019 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nommuna2/e74ee1fd95212e3aa19805e8895ff271 to your computer and use it in GitHub Desktop.
Save nommuna2/e74ee1fd95212e3aa19805e8895ff271 to your computer and use it in GitHub Desktop.
(ArcGIS API for JavaScript) Sample of adding popupTemplate to a mapservice layer in 3.x.
require(["esri/map",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/tasks/query",
"esri/tasks/QueryTask",
"esri/dijit/Popup",
"dojo/dom-construct",
"esri/dijit/PopupTemplate",
"dojo/domReady!"
], function (Map, ArcGISDynamicMapServiceLayer,Query, QueryTask, Popup, domCon, PopupTemplate) {
var map = new Map("map", {
center: [-118, 34.5],
zoom: 5,
basemap: "topo"
});
var layer = new ArcGISDynamicMapServiceLayer('http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer');
map.addLayer(layer);
var myQuery = new Query();
myQuery.where = "1=1";
myQuery.outFields = ['*'];
map.on("click", function(e){
console.log(e);
myQuery.geometry = e.mapPoint;
var queryTask = new QueryTask("http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/2");
queryTask.execute(myQuery, function(result){
console.log(result);
var popupTemplate = new PopupTemplate({
title: "My popup",
fieldInfos: [
{ fieldName: "state_name", visible: true, label: "State Name: " },
{ fieldName: "pop2000", visible: true, label: "Population: " }
]
});
result.features.forEach(function(f) {
f.setInfoTemplate(popupTemplate);
});
map.infoWindow.setFeatures(result.features);
map.infoWindow.show(e.screenPoint, map.getInfoWindowAnchor(e.screenPoint));
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment