Select to view content in your preferred language

Pass map service layer to Flex dynamically from .NET host application

2730
7
06-04-2010 05:55 AM
grahamcooke
Regular Contributor
I have a flex app that is hosted in an ASP.net application. The user must login to the ASP.net app (handled by the membership api's in the .net framework). There are other areas of the ASP.NET app that look after user management etc.. it seemed easier to do all this in .net rather than have it all in flex.

Once a user is authenticated, they may be entitled to view one or more mapping services deployed on the ARcGIS server. Their mapping choices are displayed in an ASP.NET drop down box. What i need to do is somehow get the url from that drop down box and pass it to flex in order to set the tiledmapservicelayer for <esri:Map id="MayMap"> in the flex app.

Can someone point me in the right direction or provide code snippets / links to help me please?

much appreciated!

Graham
Tags (2)
0 Kudos
7 Replies
MatejVrtich1
Occasional Contributor
Hi,
This should be done using the ExternalInterface by passing the service URL to Flex from JavaScript.

http://blog.flexexamples.com/category/externalinterface/

Matej
0 Kudos
grahamcooke
Regular Contributor
Hi Matej,

Sorry it's taken me ages to reply on here. Thanks for your assistance but I am still a bit confused. How exactly do i get the parameter from asp.net to a javascript within the flex app? I assume this is what is happening with the ExternalInterface call in the example link you attached to your reply?

I have a page (default.aspx) in .NET website that allows user to select map from dropdown
then when they click "go to flex" the following is executed:

Response.Redirect("CAWDAPT_FLEXAPP/FlexApp.aspx")

Then in FlexApp.aspx I have this html:

<body onload="resizeObjectContainer();" onresize="resizeObjectContainer();">
    <form id="form1" runat="server">
    <div>
        <object id="swfContainer" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width=100%>
            <param name="movie" value="cawdapt_v1a.swf">
       </object>
    </div>
    </form>
</body>

so the flex app is loaded inside the aspx page. How do i get the url from within asp.net into the flex app to be be used to set the basemap dynamically? Should it be passes as a paramater in the value attribute of the object element?

Also if I was using the SampleFlex viewer where is the base map set up and how would i do this dynamically?

Hope you/someone can help

cheers,

Graham
0 Kudos
Drew
by
Frequent Contributor
Graham,
You can use the ExternalInterface mentioned but maybe a simpler way would to pass the URL as a query string instead.

ASP.NET sample
Response.Redirect("CAWDAPT_FLEXAPP/FlexApp.aspx?MAP=http://www.abc.com/ArcGIS/rest/services/XYZ/MapServer")

Then on the Flex side I believe you can get the query string parameter (MAP) like so..
var mapURL:String = Application.application.parameters.MAP;



..Or just pass the Map Service name as a query string and format it to a proper AGS map URL on the Flex side...
i.e. Response.Redirect("CAWDAPT_FLEXAPP/FlexApp.aspx?MAP=XYZService")

Drew
0 Kudos
grahamcooke
Regular Contributor
hi,

I can get the map url from .net into flex now using the advice given on this thread. However I am struggling getting the map to actually display.

I know flex has got the correct url because I can display it in an alert box in the init routine :

private function init():void

            {
                  //var mapLink:String = Application.application.parameters.MAP;
                  myMapURL = Application.application.parameters.MAP;
                  Alert.show(myMapURL, "the url is");
             }


I tried passing the url directly in the mxml like so:
<esri:Map id="myMap" width="100%" height="100%" mapClick="onMapClick(event)" creationComplete="init()"

        openHandCursorVisible="false">       
              <esri:ArcGISDynamicMapServiceLayer
                  url="{Application.application.parameters.MAP}">
              </esri:ArcGISDynamicMapServiceLayer>
        </esri:Map>


but this doesnt work, so I tried adding the layers in action script, I added a call to addMapLayers in the init function above then wrote the function:

private function addMapLayers():void

            {
                  //base map layer
                  dynMaplayer = new ArcGISDynamicMapServiceLayer(myMapURL.toString());
                        //dynMaplayer.url = myMapURL;
                        dynMaplayer.id = "baseMap";
                        dynMaplayer.name = "baseMap";
                        dynMaplayer.visible = true;
                        myMap.addLayer(dynMaplayer);
              }


Still no joy. Please help - What am I doing wrong (or not doing!!)

thanks,

Graham
0 Kudos
grahamcooke
Regular Contributor
I think I am getting to the bottom of whats wrong here. I think it is something to do with the extent of the map service i am dynamically adding not being explicitly set.

I'm not really sure how to explicitly set the extent in code and also how i would get it in order to pass through the extent to the flex app from .NET, can anyone help here please?
0 Kudos
Drew
by
Frequent Contributor
Its a little different than I originally explained.
Below is a sample app that accepts a url param..


<?xml version="1.0" encoding="utf-8"?>
<mx:Application creationComplete="init()"  xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:esri="http://www.esri.com/2008/ags">
 
 <mx:Script>
  <![CDATA[
   import com.esri.ags.events.MapEvent;
   import mx.controls.Alert;
   
   [Bindable]
   private var mapURL:String;
   
   private function init():void
   {
    var search:String = ExternalInterface.call("location.search.substring", 1);
    var urlVars:URLVariables = new URLVariables();
    if (search && search.length > 0)                     
    {                         
     urlVars.decode(search);  
     mapURL = urlVars.MAP   
     myMapLayer.url = mapURL; 
    }
   }
   
  ]]>
 </mx:Script>

 <esri:Map id="map" x="10" y="78" height="682">
 <esri:ArcGISDynamicMapServiceLayer id="myMapLayer" x="254" y="294"/>
 </esri:Map>
</mx:Application>



You call this with a URL containing...

.... ?MAP=http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer

Hope this helps.

Drew
0 Kudos
grahamcooke
Regular Contributor
Cheers for this Drew,

Got it working 🙂
0 Kudos