ESRI_ELEVATION_WORLD GetElevationAtLongLat example

2977
9
02-28-2014 09:29 AM
DorothyMortenson
Occasional Contributor II
I must be missing something easy, as there are no examples of this.

I would like to retrieve the value of the elevation for a given lat/long. Do I really need a proxy or can't I just use the ESRI_Elevation_World -> GetElevationAtLonLat service?

Here's my code:


 var requestHandle = esriRequest({
            "url": "http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/MapServer/exts/ElevationsSOE/ElevationLayers/1/GetElevationAtLonLat",
            "content": {
              "lon": longitude,
              "lat": latitude,
              "format": "json"
            },
            "handeAs": "json",
            "callbackParamName": "callback"
       });
       requestHandle.then(requestSucceeded, requestFailed);  
   }
      
   function requestSucceeded(data){
          elev = JSON.parse(data);
          arrayUtils.forEach(elev, function(e) {
              var elev_meter = e.elevation;
              elev_ft = elev_meter *  3.2808399; 
              elevation += elev_ft ;
              alert(elevation);              
          });
   }


Thank you.
0 Kudos
9 Replies
JeffPace
MVP Alum
I must be missing something easy, as there are no examples of this.

I would like to retrieve the value of the elevation for a given lat/long. Do I really need a proxy or can't I just use the ESRI_Elevation_World -> GetElevationAtLonLat service?

Here's my code:


 var requestHandle = esriRequest({
            "url": "http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/MapServer/exts/ElevationsSOE/ElevationLayers/1/GetElevationAtLonLat",
            "content": {
              "lon": longitude,
              "lat": latitude,
              "format": "json"
            },
            "handeAs": "json",
            "callbackParamName": "callback"
       });
       requestHandle.then(requestSucceeded, requestFailed);  
   }
      
   function requestSucceeded(data){
          elev = JSON.parse(data);
          arrayUtils.forEach(elev, function(e) {
              var elev_meter = e.elevation;
              elev_ft = elev_meter *  3.2808399; 
              elevation += elev_ft ;
              alert(elevation);              
          });
   }


Thank you.


Looks like it works fine

sampleserver4.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/MapServer/exts/El...

Have you tried outputting your result (data) in requestSucceeded? I don't thing its an array, its a json object.

So it should be (guessing as i dont have a test environment)

   function requestSucceeded(data){
          result= JSON.parse(data);
          elevation = result.elevation;

   }
0 Kudos
DorothyMortenson
Occasional Contributor II
I'm afraid that did not work.

Here's what I'm getting in Chrome:

XMLHttpRequest cannot load http://sampleserver4.arcgisonline.com/ArcGIS/rest/info?f=json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://apps.wrd.state.or.us' is therefore not allowed access. index.html:1
Resource interpreted as Script but transferred with MIME type text/html: "http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_E�?�094&format=json&callback=dojo.io.script.jsonp_dojoIoScript9._jsonpCallback". init.js:1035
Uncaught SyntaxError: Unexpected token < 

0 Kudos
JohnGravois
Frequent Contributor
hi dorothy,

just a gentle reminder that our sample servers are not meant to be utilized in production applications and that individual services may go down temporarily or be moved/deleted permanently without notice.

our newest generation producation elevation service doesn't burn credits, but requires an ArcGIS Organizational login to utilize.  please check out the sample I provided in this forum thread to see how it works.
0 Kudos
DorothyMortenson
Occasional Contributor II
I was never able to get this example to work.
0 Kudos
JohnGravois
Frequent Contributor
hi Dorothy,
which example are you referring to?  your own code or the sample I provided in the linked forum thread?

if you're talking about http://jsfiddle.net/jagravois/PFART/, what exactly are you seeing?
0 Kudos
DorothyMortenson
Occasional Contributor II
I am referring to your example.

I click on the map, and am asked to log in. So I do so.

I click on the map again and nothing happens.

I can see the code tho. Thank you. 


I was able to finally get something to work.  Here is the code:
function getElevation(latitude, longitude) {
    elevurl = decodeURIComponent("http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/MapServer/exts/ElevationsSOE/ElevationLayers/1/GetElevationAtLonLat");
    elev_uri = elevurl + "?lon=" + longitude.toString() + "&lat=" + latitude.toString() + "&f=pjson";

      var requestHandle = esriRequest({
          url: elev_uri,
           handeAs: "json",
           callbackParamName: "callback"
       });
       requestHandle.then(requestSucceeded, requestFailed);  
   } 

function requestSucceeded(data) {
    elev_meter = data.elevation;
    elev_ft       = elev_meter * 3.2808399;
}
          
function requestFailed(error, io){
          dojoJson.toJsonIndentStr = " ";
          dom.byId("message").value = dojoJson.toJson(error, true);
}



Thank you.
0 Kudos
JohnGravois
Frequent Contributor
Dorothy,

i dont have a better guess than it having something to do with your credentials, because when i sign in a popup displays showing the elevation.  what happens when you sign in to arcgis.com?  are you sure you have an organizational subscription?

either way, im glad to hear you have some working code that does what you need.
0 Kudos
KenBuja
MVP Esteemed Contributor
I have two ArcGIS.com accounts, one is from my Esri Global Account and another is an organizational account. The popup box will only work when I log into the organization account.
0 Kudos
JohnGrayson
Esri Regular Contributor
Here's another attempt at a jsfiddle that should work: http://jsfiddle.net/99vD9/5/

var elevationServiceUrl = "http://sampleserver4.arcgisonline.com/ArcGIS/rest/services/Elevation/ESRI_Elevation_World/MapServer/exts/ElevationsSOE/ElevationLayers/1/GetElevationAtLonLat"

    dojo.connect(map, 'onClick', function (evt) {
        esri.request({
            url: elevationServiceUrl,
            content: {
                lon: evt.mapPoint.getLongitude(),
                lat: evt.mapPoint.getLatitude(),
                f: "json"
            },
            callbackParamName: "callback",
        }).then(function (response, io) {
            alert("Elevation = " + response.elevation.toFixed(1) + " meters");
        }, function (error, io) {
            console.error("Unable to get elevation", error);
            alert("Unable to get elevation...");
        });
    });
0 Kudos