I've created a script tool, geoprocessing service that takes three input parameters as strings; latitude, longitude and location. Using the GP tool in popup as an example, I'm trying to pass the lat/lon strings in from a click event, and the location name from a user prompt. I've looked at the API reference for GP service parameters, but I'm not sure if I'm getting the syntax right. The only sandbox examples I can find take features or geometries as inputs, but not strings. When I test this, I get the error "TypeError: Cannot set property 'innerHTML' of null" as soon as the map loads.
I'm also thinking I need to make my variables mp, x, and y global so they can be passed in to the different functions.
Here is the section of my script:
map.on("click", function(evt){
var mp = webMercatorUtils.webMercatorToGeographic(evt.mapPoint);
var x = mp.x.toFixed(3);
var y = mp.y.toFixed(3);
//var location = prompt("Enter Location Name", "Location");
map.graphics.clear();
var graphic = new Graphic(evt.mapPoint);
map.graphics.add(graphic);
map.infoWindow.setFeatures([graphic]);
map.infoWindow.setContent(" Longitude: " + x.toString() + " <br>Latitude: " + y.toString());
map.infoWindow.show(evt.mapPoint)
});
var link = domConstruct.create("a",{
"class": "action",
"id": "statsLink",
"innerHTML": "Chart Test", //text that appears in the popup for the link
"href": "javascript: void(0);"
}, query(".actionList", map.infoWindow.domNode)[0]);
on(link, "click",createChart);
domAttr.set(dom.byId("statsLink"), "innerHTML", "Calculating...");
window.gp_chart = new Geoprocessor("http://gis.mymetcon.com/arcgis/rest/services/Chart_Test/GPServer/ChartTest");
function createChart(evt){
var mp = webMercatorUtils.webMercatorToGeographic(evt.mapPoint);
var x = mp.x.toFixed(3);
var y = mp.y.toFixed(3);
var lat,lon,location = new GPString();
lat.value = x.toString();
lon.value = y.toString();
location.value = prompt("Enter Location Name", "Location");
var taskParams = {
"Latitude":lat,
"Longitude":lon,
"Location":location
};
window.gp_chart.execute(taskParams);
};
Solved! Go to Solution.
Lloyd,
If you study the code you will find that you are adding a graphic to the map based on the map click event and then setting the popups selected feat to be equal to this graphic. So the line 4 in my suggested code gets the popups selected feature and extracts the x and y from that. The issue is you are var lat and lon and then try to set those vars value property (missed that mistake the first time).
Try:
on(link, "click", function(){
domAttr.set(dom.byId("statsLink"), "innerHTML", "Calculating...");
window.gp_chart = new Geoprocessor("http://<ourserver>/arcgis/rest/services/Chart_Test/GPServer/ChartTest");
var feature = window.map.infoWindow.getSelectedFeature();
var mp = webMercatorUtils.webMercatorToGeographic(feature.geometry);
var x = mp.x.toFixed(3);
var y = mp.y.toFixed(3);
var lat,lon,location = "";
lat = x;
lon = y;
location = prompt("Enter Location Name", "Location");
var taskParams = {
"Latitude": lat,
"Longitude": lon,
"Location": location
};
window.gp_chart.execute(taskParams);
});
Lloyd,
The issue I see is that you do not have your link click function defined:
on(link, "click", function(){
domAttr.set(dom.byId("statsLink"), "innerHTML", "Calculating...");
window.gp_chart = new Geoprocessor("http://<ourserver>/arcgis/rest/services/Chart_Test/GPServer/ChartTest");
var feature = window.map.infoWindow.getSelectedFeature();
var mp = webMercatorUtils.webMercatorToGeographic(feature.geometry);
var x = mp.x.toFixed(3);
var y = mp.y.toFixed(3);
var lat,lon,location = "";
lat.value = x;
lon.value = y;
location = prompt("Enter Location Name", "Location");
var taskParams = {
"Latitude":lat,
"Longitude":lon,
"Location":location
};
window.gp_chart.execute(taskParams);
});
Thanks! I made the changes and I'm getting this error for lat.value = x; "Cannot set property 'value' of undefined
at HTMLAnchorElement.<anonymous> "
I'm wondering if this is because the click in this function is an html link in the popup, not a map click? So it's trying to calculate feature geometries an event that is not a feature. In which case, I will need to pass in the x and y variables from the first map click function. I want to display the lat lon coordinates in the popup and then have the user click the link to the tool.
Lloyd,
If you study the code you will find that you are adding a graphic to the map based on the map click event and then setting the popups selected feat to be equal to this graphic. So the line 4 in my suggested code gets the popups selected feature and extracts the x and y from that. The issue is you are var lat and lon and then try to set those vars value property (missed that mistake the first time).
Try:
on(link, "click", function(){
domAttr.set(dom.byId("statsLink"), "innerHTML", "Calculating...");
window.gp_chart = new Geoprocessor("http://<ourserver>/arcgis/rest/services/Chart_Test/GPServer/ChartTest");
var feature = window.map.infoWindow.getSelectedFeature();
var mp = webMercatorUtils.webMercatorToGeographic(feature.geometry);
var x = mp.x.toFixed(3);
var y = mp.y.toFixed(3);
var lat,lon,location = "";
lat = x;
lon = y;
location = prompt("Enter Location Name", "Location");
var taskParams = {
"Latitude": lat,
"Longitude": lon,
"Location": location
};
window.gp_chart.execute(taskParams);
});
Thanks. I made the change and it's working. It just hangs on "calculating" though. Do I need to add some kind of task kill when it's finished so it reverts back to the original text "Chart Test"?
Lloyd,
Sure just like the sample does you need to set the statsLink innerHTML back one the gp executes successfully.
Oh right. Thanks!