Select to view content in your preferred language

Enhanced Bookmark - Save Bookmarks

3212
11
Jump to solution
12-06-2012 03:38 AM
JeffersonFerreira_Ferreira
Deactivated User
Dear Colleagues;

I'm using the Enhanced Bookmark Widget (http://www.arcgis.com/home/item.html?id=ed4d8dbef33d400499d2cc6475f24a4e) and it's great. But i'm trying to make a little adaptation with no success. Instead save only the user-defined bookmarks I want to save all bookmarks - including the bookmarks defined in xml by the developer. By default it's not possible (only user defined), but it's quite simple, I guess. If someone can give me a hand I will be greatful. Below part of the mxml the execute the save function.


// mjk - Enhanced Bookmark    /**     * Parses bookmarks from the default widget config file or      * a file loaded by the user. In the case of a user created      * config file set isUserCreated to true so that they behave the      * same as bookmarks added at run time.      **/    private function parseBookmarkConfig(config:XML, isUserCreated:Boolean):void    {     if(config)     {      var bookmarkList:XMLList = config..bookmark;      var length:int = bookmarkList.length();            if(length < 1)      {       Alert.show("Nenhuma marcação válida encontrada!", wTemplate.widgetTitle);       return;      }            for (var i:int = 0; i < length; i++)      {       // mjk- Enhanced Bookmark       var comment:String = bookmarkList.@comment;             var name:String = bookmarkList.@name;       var extent:String = bookmarkList;       var extArray:Array = extent.split(" ");       var bookmark:Bookmark = new Bookmark();             //mjk - Enhanced Bookmark       bookmark.comment = comment;       bookmark.userCreated = isUserCreated;              bookmark.name = name;       bookmark.xmin = Number(extArray[0]);       bookmark.ymin = Number(extArray[1]);       bookmark.xmax = Number(extArray[2]);       bookmark.ymax = Number(extArray[3]);       bookmarkAL.addItem(bookmark);              // mjk - Enhanced Bookmark        // Add user created bookmarks to bookmarkSOAL       if(isUserCreated)       {        bookmarkSOAL.addItem(bookmark);       }      }     }    }                 private function addBookmark():void             {                 var name:String = txtName.text;                 if (name)                 {                     var ext:Extent = map.extent;                     var bookmark:Bookmark = new Bookmark();                     bookmark.name = name;            // mjk - Enhanced Bookmark      bookmark.comment = txtAreaComment.text;                           bookmark.userCreated = true;                     bookmark.xmin = Number(ext.xmin.toFixed(4));                     bookmark.ymin = Number(ext.ymin.toFixed(4));                     bookmark.xmax = Number(ext.xmax.toFixed(4));                     bookmark.ymax = Number(ext.ymax.toFixed(4));                     bookmarkAL.addItem(bookmark);                     bookmarkSOAL.addItem(bookmark);                     saveBookmarks();                     txtName.text = "";            // mjk - Enhanced Bookmark      txtAreaComment.text = "";                           bkmList.addEventListener(EffectEvent.EFFECT_END, bkmList_effectEndHandler);                     showBookmarksList();                 }                 else                 {                     Alert.show(errorLabel, wTemplate.widgetTitle);                 }             }              private function bkmList_effectEndHandler(event:EffectEvent):void             {                 bkmList.removeEventListener(EffectEvent.EFFECT_END, bkmList_effectEndHandler);                 // scroll to the bottom                 bookmarkDG.verticalScrollPosition += bookmarkDG.layout.getVerticalScrollPositionDelta(NavigationUnit.END);             }              private function removeBookmark(event:Event):void             {                 var bookmark:Bookmark = ItemRenderer(event.target).data as Bookmark;                 bookmarkAL.removeItem(bookmark);                 bookmarkSOAL.removeItem(bookmark);                 saveBookmarks();             }              private function saveBookmarks():void             {                 if (bookmarkSO)                 {                     try                     {                         bookmarkSO.flush();                     }                     catch (err:Error)                     {                         trace(err);                     }                 }             }              private function showBookmark(event:Event):void             {                 var bookmark:Bookmark = ItemRenderer(event.target).data as Bookmark;                 var ext:Extent = new Extent(bookmark.xmin, bookmark.ymin, bookmark.xmax, bookmark.ymax);                 map.extent = ext;             }        // mjk - Enhanced Bookmark    /**     * Saves user created bookmarks to a file.     * The xml schema is the same as the widgets.     **/    private function saveBooksMarksToFile():void    {     if(bookmarkSOAL)     {      var xml:XML = createBookmarksXML();            if(!xml) return;            fileReference = new FileReference();      fileReference.save(xml, "LocaisWebSIG.txt");     }    }    
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinDruzgala
Frequent Contributor
Ok I've tested it on my app and everything is working smoothly:) Few things i suggest you to do:
1. In Flash Builder click Project->Clean....
2.Rebuild the project.
3.If it's still not working download again the enhanced bookmark widget and put it in your project.

4.Now change the code in createBookmarksXML function:
for(var j:int = 0; j < bookmarkAL.length; j++) {  var bookmark:Bookmark = bookmarkAL.getItemAt(j) as Bookmark;  result.appendChild(bookmark.toXML()); }

5.Run the application, create few bookmarks and the save the file.

In attachments you will find source code of enhanced bookmark widget I've used and bookmarks.xml file which is a file created with this widget!

Cheers
MDruzgala

View solution in original post

0 Kudos
11 Replies
MarcinDruzgala
Frequent Contributor
Hi here are my thoughts ; ) Here is the createBookmarksXML function:
private function createBookmarksXML():XML
{
 var length:int = bookmarkSOAL.length;
 if(length < 1)
 {
  Alert.show("There are no user defined bookmarks to export!", wTemplate.widgetTitle);
  return null;
 }
 
 var result:XML = <bookmarks></bookmarks>;
 result.@name = "userbookmarks";
 
 for(var i:int = 0; i < length; i++)
 {
  var bookmark:Bookmark = bookmarkSOAL.getItemAt(i) as Bookmark;
  result.appendChild(bookmark.toXML());
 }
 
 return result;
}

So the bookmarks that are written into xml file are taken from bookmarkSOAL (ArrayList) so what you have to do is just add bookmarks provided by the developer in bookmark widget config file(if I understand the code correctly they are stored in bookmarkAL (ArrayList)). I would try to do this that way:
private function createBookmarksXML():XML
{
 var length:int = bookmarkSOAL.length;
 if(length < 1)
 {
  Alert.show("There are no user defined bookmarks to export!", wTemplate.widgetTitle);
  return null;
 }
 
 var result:XML = <bookmarks></bookmarks>;
 result.@name = "userbookmarks";
 
 for(var i:int = 0; i < length; i++)
 {
  var bookmark:Bookmark = bookmarkSOAL.getItemAt(i) as Bookmark;
  result.appendChild(bookmark.toXML());
 }
 for(var j:int = 0; i < bookmarkAL.length; j++)
 {
  var bookmark:Bookmark = bookmarkAL.getItemAt(j) as Bookmark;
  result.appendChild(bookmark.toXML());
 }

 return result;
}


Didn't check it if it works but you can try:)
Good luck!

MDruzgala
0 Kudos
JeffersonFerreira_Ferreira
Deactivated User
MDruzgala;

Thanks for your agile reply. I tried it and, unfortunately, with no success. The message "There are no user defined bookmarks to export!" still appears.

And one additional thing is that always we refresh the page or reopen the application the user-defined bookmarks appear with "bug", without your name and the red "x" to delete the bookmark doesnt work. The save and e-mail bookmark doesnt work too unless we clean cookies.

With you or others have a tip, will be great! Attached the entire mxml. Thank you.
0 Kudos
MarcinDruzgala
Frequent Contributor

And one additional thing is that always we refresh the page or reopen the application the user-defined bookmarks appear with "bug", without your name and the red "x" to delete the bookmark doesnt work. The save and e-mail bookmark doesnt work too unless we clean cookies.


Is it happening with normal bookmark widget too?

MDruzgala
0 Kudos
JeffersonFerreira_Ferreira
Deactivated User
Is it happening with normal bookmark widget too?

MDruzgala



I've tested right now. With the "normal" widget it doesnt happens. In other words, it works fine - we add a bookmark, close the tab, reopen the application and all are there and working. But the Enhanced no, like I wrote in the last post. Any idea??
0 Kudos
MarcinDruzgala
Frequent Contributor
Ok I've tested it on my app and everything is working smoothly:) Few things i suggest you to do:
1. In Flash Builder click Project->Clean....
2.Rebuild the project.
3.If it's still not working download again the enhanced bookmark widget and put it in your project.

4.Now change the code in createBookmarksXML function:
for(var j:int = 0; j < bookmarkAL.length; j++) {  var bookmark:Bookmark = bookmarkAL.getItemAt(j) as Bookmark;  result.appendChild(bookmark.toXML()); }

5.Run the application, create few bookmarks and the save the file.

In attachments you will find source code of enhanced bookmark widget I've used and bookmarks.xml file which is a file created with this widget!

Cheers
MDruzgala
0 Kudos
JeffersonFerreira_Ferreira
Deactivated User
Dear MDruzgala;

Great! Your code works like a charm! But my bookmark is behaving very strangely. That problem persists - I add bookmarks, and when the window/tab is closed e reopened the delete user-defined bookmarks doesnt work anymore, the name doesnt appear and the save/e-mail bookmarks button does nothing.
I download the widget again (http://www.arcgis.com/home/item.html...d2cc6475f24a4e), download your mxml attached last message and try to use and the error even persists. I will attach the folder of my uncompiled widget, could test compiling and using it, please?
0 Kudos
MarcinDruzgala
Frequent Contributor
Well I can't reproduce your problem...Which browser are you using? Did you try your application on another computer?
0 Kudos
JeffersonFerreira_Ferreira
Deactivated User
Well I can't reproduce your problem...Which browser are you using? Did you try your application on another computer?


MDruzgala;

I've identified the problem. It was the sdk compiler version. I was using the flex 4.6 version, so I tried the 4.5.1, 4.5A and finally the 4.1A, that worked. By curiosity, what compiler version you used with my code?

Only one issue I'll solve is the possibility to save bookmarks without the need to create (user-defined)bookmarks. But is a quite simple I guess. I'll comment some parts of the code and give you a feedback soon.
0 Kudos
JeffersonFerreira_Ferreira
Deactivated User
Dear Druzgala;

Thanks a lot. The code is working great! Now the user don't need to create a custom bookmark to save the default ones and all bookmarks are saved, including the "developer-defined".
Your name are in the comments of my code.

My best regards!



private function createBookmarksXML():XML
   {
    var length:int = bookmarkSOAL.length;
    /*if(length < 1)
    {
     Alert.show("Não há locais definidos pelo usuário para exportar!", wTemplate.widgetTitle);
     return null;
    }*/ // Aqui a condição foi comentada para permitir que o usuário exporte os bookmarks sem a necessidade de criar nenhum **Jecogeo 
    
    var result:XML = <bookmarks></bookmarks>;
    
    result.@name = "bookmarks";
    
    /*for(var i:int = 0; i < length; i++)
    {
    var userBookmark:Bookmark = bookmarkSOAL.getItemAt(i) as Bookmark;
    result.appendChild(userBookmark.toXML());
    }*/
    
    for(var j:int = 0; j < bookmarkAL.length; j++)
    {
     var preDefBookmark:Bookmark = bookmarkAL.getItemAt(j) as Bookmark;
     result.appendChild(preDefBookmark.toXML());
    }
    return result; // Com a ajuda de Marcin Druzgala (Polônia) esse for foi inserido para permitir que o usuário exporte TODOS os bookmarks e não somente aqueles por ele criados **Jecogeo
0 Kudos