Getting the Map Service REST URL to the Map as URL parameter

3851
7
08-04-2011 04:33 PM
DonBarker
Occasional Contributor
I'm a javascript novice.

Undaunted, I would like to pass the dynamic map service URL parameter here

var operationalLayer = new esri.layers.ArcGISDynamicMapServiceLayer.toString("string_goes_here");

as a URL parameter, rather than writing it explicitly into the html file as in the simple JS map viewer examples.

I'm grabbing the URL ok using the function shown below.  I'd like to simply pass the contents of the var from that function into the operationalLayer variable like this:

var operationalLayer = new esri.layers.ArcGISDynamicMapServiceLayer(rest_url);

Or like this:

var operationalLayer = new esri.layers.ArcGISDynamicMapServiceLayer.toString(rest_url);

But neither of those approaches work, of course.  Can someone point the way?   (Hopefully, short and straight.)

Thanks,
Don

  function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
var rest_url = gup( 'resturl' );
0 Kudos
7 Replies
StephenLead
Regular Contributor III
I can't test this (and I'm not an expert on regexp), but I think you might want to do something along these lines:


var fullUrl = window.location.href; //get the full URL
var layerUrl = gup(fullUrl); // send this to the function
console.log(layerUrl);// this should be just the layer definition section

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec(name);
  if( results == null )
    return "";
  else
    return results[1]; //return just the Layer definition of the URL
}



Good luck,
Steve
0 Kudos
DonBarker
Occasional Contributor
Hey Steve,

Thanks for taking time to reply.

I'm already getting the map service REST URL as a string variable ok.  So the regex is not a problem.

But how to I pass that string to this variable?

var operationalLayer = new esri.layers.ArcGISDynamicMapServiceLayer("string_goes_here");

Thanks again,
Don
0 Kudos
StephenLead
Regular Contributor III

But how to I pass that string to this variable?
var operationalLayer = new esri.layers.ArcGISDynamicMapServiceLayer("string_goes_here");


Hi Don,

Sorry, I misunderstood your question. What is the value of rest_url after you've sent it through your function?

The syntax for specifying the URL parameter is shown here.

The URL should be something like http://server/ArcGIS/rest/services/layername - see the Services Directory help file entry.

Cheers,
Steve
0 Kudos
DonBarker
Occasional Contributor
0 Kudos
StephenLead
Regular Contributor III
The value is a clean URL like this:

http://129.2.24.163/ArcGIS/rest/services/00_SMC_Environmental_Health/MapServer


I'm a bit confused now, since this works:

var rest_url = "http://129.2.24.163/ArcGIS/rest/services/00_SMC_Environmental_Health/MapServer";
var dynamicMapServiceLayer = new esri.layers.ArcGISDynamicMapServiceLayer(rest_url); 
map.addLayer(dynamicMapServiceLayer);


Can you examine rest_url at run-time in FireBug and ensure that it's a valid string?

Sorry I haven't been able to help much 🙂

Steve
0 Kudos
DonBarker
Occasional Contributor
Steve,

I confused you because Im a poor javascripter.  I believed that this could not work:

var dynamicMapServiceLayer = new esri.layers.ArcGISDynamicMapServiceLayer(rest_url);

You have affirmed that it should work.  So now I see how to pursue this.  I need to prepend "http://" to the var rest_url.

Thanks so much for helping me muddle through this.
0 Kudos
StephenLead
Regular Contributor III
no probs, happy to have helped. I'll be interested to see the finished result if you can post it here.
0 Kudos