Select to view content in your preferred language

Web AppBuilder, URL To Open Map @ Lat/Long or Address?

5545
14
09-11-2015 07:18 AM
DanAllen
Frequent Contributor

If I have a Web AppBuilder application that is off the shelf and hosted in ArcGIS.com (not developer edition), is there a way tack on a lat/long or address to the end of the map URL, to get it to open the map at a location other than the default initial map extent?

Thanks

0 Kudos
14 Replies
RobertScheitlin__GISP
MVP Emeritus

Michael,

   It is very convoluted as WAB is by no means a simple JS app. This functionality is broken up across 4 or more js files.

0 Kudos
MichaelVolz
Esteemed Contributor

Robert:

Do you think this is a difficult piece of functionality (code-wise) to replicate in a non-Web App Builder javascript web app?

Do you know if any of the ESRI samples on the javascript api show how to enable this functionality?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Michael,

  Sure here is a sample:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Simple Map with url parameters</title>
    <link rel="stylesheet" href="http://js.arcgis.com/3.14/esri/css/esri.css">
    <style>
      html, body, #map {
        height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
      }
      body {
        background-color: #FFF;
        overflow: hidden;
        font-family: "Trebuchet MS";
      }
    </style>
    <script src="http://js.arcgis.com/3.14/"></script>
    <script>
      var map;

      require([
        "esri/map",
        "dojo/io-query",
        "dojo/_base/lang",
        "esri/geometry/Point",
        "esri/tasks/GeometryService",
        "esri/SpatialReference",
        "esri/tasks/ProjectParameters",
        "dojo/on",
        "dojo/domReady!"
      ], function(
        Map,
        ioquery,
        lang,
        Point,
        GeometryService,
        SpatialReference,
        ProjectParameters,
        on
      ) {
        map = new Map("map", {
          basemap: "topo",  //For full list of pre-defined basemaps, navigate to http://arcg.is/1JVo6Wd
          center: [-122.45, 37.75], // longitude, latitude
          zoom: 13
        });

        on(map, "load", function(){
          setUrlParams();
        });

        function getUrlParams () {
          var s = window.location.search,
            p;
          if (s === '') {
            return {};
          }
          p = ioquery.queryToObject(s.substr(1));
          return p;
        }

        function setUrlParams () {
          var myObject = getUrlParams();
          var centerPnt, level;
          if (myObject.center && !lang.isArrayLike(myObject.center)) {
            var centerArr = myObject.center.split(",");
            var sr;
            if(centerArr[2]){
              sr = new SpatialReference({ wkid: centerArr[2] });
            }else{
              sr = new SpatialReference({ wkid: 4326 });
            }
            centerPnt = new Point(centerArr[0], centerArr[1], sr);
          }
          if (myObject.level) {
            level = myObject.level || 12;
          }
          if(!centerPnt.spatialReference.equals(map.spatialReference)){
            projectCenter(centerPnt, level);
            return;
          }
          map.centerAndZoom(centerPnt, level).then(function(){
            console.info("Zoomed to center");
          });
        }

        function projectCenter(centerPnt, level){
          var params = new ProjectParameters();
          params.geometries = [centerPnt];
          params.outSR = map.spatialReference;
          var gsvc = new GeometryService("http://sampleserver6.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
          gsvc.project(params, lang.hitch(this, function(result){
            map.centerAndZoom(result[0], level).then(function(){
              console.info("Zoomed to projected center");
            });
          }), lang.hitch(this, function(err){
            console.error(err);
          }));
        }

      });
    </script>
  </head>

  <body>
    <div id="map"></div>
  </body>
</html>
MichaelVolz
Esteemed Contributor

Thank you very much Robert

Is there a trick to copying this code snippet into NotePad without having the line numbers copied in as well?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Dan Allen​,

   Dont forget to mark this thread as answered by clicking on the "Correct Answer" button in the post that answered your question.

0 Kudos