FlexViewer 2.1: Add Layer to the map when a widget is activated

991
8
10-05-2010 03:18 AM
BrendanCunningham
New Contributor
Hello,
I am trying to add some nice additional functionality to the Flex Viewer 2.1 Streetview widget (by Robert Scheitlin).

In Google Maps, when Streetview is activated a blue layer appears over all areas where Streetview photography is available. I want to replicate this on a Flex Viewer application. Our administrative area is quite small and it will be a quick task to create a layer that shows all our Streetview-ready streets.

My aim is to add our layer showing the Streetview locations when the Streetview widget is activated. In turn, when the widget is closed/minimized then the layer will disappear from the map.

I am guessing this is simple enough (for Flex professionals)... although I can't figure it out as a Flex novice.

Any help would be greatly appreciated.

Thanks,

Brendan
Tags (2)
0 Kudos
8 Replies
RobertScheitlin__GISP
MVP Emeritus
Brendan,

   it is not going to be to hard if you have the map service published to your ArcGIS Server than all you have to do is add the map service in the widgetOpenedHandler function and remove it in the widgetClosedHandler.  So something like this

//Add this to the top with the other private vars
private var gsLayer:ArcGISDynamicMapServiceLayer;
//This goes in your init or what ever the function is called that is fired by creationComplete
gsLayer = new ArcGISDynamicMapServiceLayer("Yor url");
//This goes in your widgetOpenedHandler 
map.addLayer(gsLayer,-1);
//Then on the close do
map.removeLayer(gsLayer);
0 Kudos
BrendanCunningham
New Contributor
Hi Robert,
The good news is that I can compile the project with these code changes without any errors.
The bad news is that the layer is not added to the map when I load the Streetview widget.
The steps i have taken are as follows:

I placed the following line under the import section and above the start of private/public references:
private var gsLayer:ArcGISDynamicMapServiceLayer;


The following code is the "addStatList()" function I am using, because the Streetview widget seems to  reference this instead of init():
   private function addStatList():void
   {
    ViewerContainer.addEventListener(AppEvent.SET_STATUS, statusSet);
    theStatus = "Show Google Street View";
    gsLayer = new ArcGISDynamicMapServiceLayer("http://SERVER/ArcGIS/rest/services/WebMercator/Roads/MapServer/0");
   }
   


The OpenHandler is as follows:
 
private function widgetOpenedHandler(event:Event):void
   {
    google();
    map.addLayer(gsLayer,-1);

   }


The ClosedHandler is as follows:
 
   private function widgetClosedHandler(event:Event):void
   {
    map.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    CursorManager.removeAllCursors();
    map.panEnabled = true;
    map.cursorManager.removeCursor(map.cursorManager.currentCursorID);
    setMapNavigation(null, null);
    map.cursorManager.removeCursor(map.cursorManager.currentCursorID);
    map.removeLayer(gsLayer);    
    
   }


Not sure what I have done wrong with this.
I've also tried the URL as a full service (ie, without the "/0" at the end) and there is no joy here either.
Thanks as ever for your help.
Brendan
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Brendan,

   I know that you don not want /0 but why it is not working when you removed that I am not sure. Do you have a layer list widget or some widget that shows you what layers are in the map and if so do you see the new layer listed?
0 Kudos
BrendanCunningham
New Contributor
Hi Robert,
I sorted out the issue...
The following line
map.addLayer(gsLayer,-1);

should appear in the
addStatList()
section.
The layer successfully added to the map when I placed the line in that section.

Thankis for all your help with this.
0 Kudos
BrendanCunningham
New Contributor
Hi Robert,
I sorted out the issue...
The following line
map.addLayer(gsLayer,-1);

should appear in the
addStatList()
section (or the init() section).
The layer successfully added to the map when I placed the line in that section.
it is called "ArcGISDynamicMapServiceLayer495", but I'm not too concerned about this.

Thanks for all your help with this.
It is ideal and adds real value to the Streetview widget.
My point of view is that when people use Google Streetview on Google Maps the roads display blue and users are familiar with this. However I'd be concerned that users who are not overly familiar with Streetview might click on houses to see what's inside!!!
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Brendan,

   If you want to change the "ArcGISDynamicMapServiceLayer495" than all you have to do is set the

gsLayer.name = "whatever";
0 Kudos
BrendanCunningham
New Contributor
That's perfect Robert - thanks again.
0 Kudos
RobertMyers
New Contributor
I think I followed the above instructions correctly and added the code to the "Google Street View Widget" http://www.arcgis.com/home/item.html?id=f069d8a3aa304e29a432b48e5f64ca74

I just have one problem. The layer will not be added unless I open and close the widget, then open it again.

One thing that might be different, I added "gsLayer = new ArcGISDynamicMapServiceLayer("URL");" to the init() function......widgetConfigLoaded="init()". I am not sure if this is correct. There doesn't appear to be an "creationComplete="init()" in the StreetViewWidget.mxml file. Maybe this needs to be somewhere else.

Also, is there a way to exclude the layer being added from the TOC?

Thank you.

Edit: Seems to work if I have the map.addLayer(gsLayer,-1); in both the init() function and in the widgetOpenedHandler.
0 Kudos