2015-11-02 43 views
0

我正在完成我寫的應用程序的模塊化過程。這適用於空間位置如何從自定義dojo模塊獲取值?

我正在使用事件來查詢用戶的經緯度在應用程序中使用的位置。我的呼叫片段在下面(按鈕點擊啓動它)

<script> 
    require([ 
     'dojo/dom', 
     'dojo/_base/array', 
     'demo/testModule', 
     'esri/SpatialReference', 
     'esri/geometry/Point' 
    ], function (
     dom, 
     arrayUtils, 
     testModule, 
     SpatialReference, 
     Point 
    ) { 
     //Here is the button click listener 
     $('#whereAmIButton').click(function() { 
      var spatialRef = new esri.SpatialReference({ 'wkid': 4326 }); 

      //variable I want to set to a returned geometry. 
      var myGeom; 

      //This runs but I'm missing the boat on the return of a value 
      testModule.findUserLocPT(spatialRef); 

      //var myModule = new testModule(); //not a constructor 
     }); 
    }); 
</script> 

這是自定義模塊。它將信息記錄到用戶位置的控制檯。但是我想返回設置'myGeom'變量的值。

define(['dojo/_base/declare','dojo/_base/lang','dojo/dom', 
'esri/geometry/Point','esri/SpatialReference'], function (
    declare, lang, dom, Point, SpatialReference) { 
return { 
    findUserLocPT: function (spatialRef) { 
     var geom; 
     var location_timeout = setTimeout("geolocFail()", 5000); 
     navigator.geolocation.getCurrentPosition(function (position) { 
      clearTimeout(location_timeout); 

      var lat = position.coords.latitude; 
      var lon = position.coords.longitude; 

      setTimeout(function() { 
       geom = new Point(lon, lat, spatialRef); 
       //console.log writes out the geom but that isnt what I am after 
       console.log(geom); 
       //I want to return this value 
       return geom; 
      }, 500); 
     }); 
     function geolocFail() { 
      console.log("GeoLocation Failure"); 
     } 
    } 
}//end of the return 

});

任何幫助將受到歡迎。我可以通過引用來更改文檔中的text/html值,但不會將事情作爲變量返回。

安迪

回答

0

好吧,我不知道這是否是「最好」的答案,但我有一個現在。

我加了一個全局變量「的test.html頁面內

<script> 
    var theGeom; //This is the variable 
    require([ 
     'dojo/dom', 

這裏是我在原來的道場使用設置此變量的值「需要」的代碼塊。這是來自'testModule.js'

setTimeout(function() { 
    geom = new Point(lon, lat, spatialRef);          
    theGeom = geom; //Here is the feedback of the value to the global variable.     
    return myGeom; 
}, 500); 


$('#whereAmIButton').click(function() { 
    var spatialRef = new esri.SpatialReference({'wkid':4326});    
    testModule.findUserLocPT(spatialRef);    
    setTimeout(function() { 
     console.log(theGeom); //here is the value set and ready to use 
      },2000); 
}); 

我不確定這是否是最好的方法。如果你有更好的東西,請讓我知道。

Andy

相關問題