User:Mweigand

From OpenStreetMap Wiki
Jump to navigation Jump to search

My native language is German, so please correct things I mess up...

Started working on a mozilla-ubiquity command for osm to have a osm-replacement for the map command they use at the moment. It's only a small project; I don't see the necessity to create a project page outside this wiki and will use this page to organise my work here.


For those of you who don't know the Mozilla-Ubiquity project jetzt, here a few links to start:

To get to know Mozilla ubiquity, watch the introduction video they have on their website: http://ubiquity.mozilla.com/

The command authoring tutorial you can find at https://wiki.mozilla.org/Labs/Ubiquity/Authors

I think this is an amazing tool, but they use only google maps for mapping.

What is working: type "osm PLACE", and you will get the map in the preview window of that place. To find the place, mapfinder was used. The first of the results given back by the xml-frontend of mapfinder is display instantly.

TODO: - add Licence ? - add "insert" functionality, so that you can click "insert" and the map you see in the preview gets pasted in the webpage you are currently viewing (e.g. google mail)



The code I got so far:

CmdUtils.CreateCommand({
  name: "osm",
  takes: {"Display in Openstreetmap": noun_arb_text},
  
  preview: function (pblock, loco){
    /* clean innerHTML, so that there is no pile-up of maps in the preview window*/
    pblock.innerHTML = "";
    
    /* store the first node that the namefinder finds here */
    var first_lat;
    var first_lon;
    
    /* the search string */
    var sstring = escape(loco.text);
    
    /* generate the namefinder xml url */
    href = "http://gazetteer.openstreetmap.org/namefinder/search.xml?find=" + sstring;
    
    /* print information about the search string and url */
    pblock.innerHTML = pblock.innerHTML + "Suche nach: " + sstring + "<br />URL bei OSM Namefinder:<a href=\"" + href + "\">"+href+"</a><br />";
    
    /* nice website when dealing with xml parsing with ajax for the first time
        Referenz-XML Datei, siehe
        http://www.w3schools.com/DOM/dom_document.asp
    */
    
    /* query namefinder and parse the resulting xms page */
    jQuery.ajax({  
                async: false,  
                type: "GET",  
                url: href,  
                dataType: "xml",
                timeout: 1,
                /* namefinder sometimes is slow or not responding. I would like to show an notification in the preview window if that happens.
                   Seting timeout to 2 seconds would be a good thing, I think
                */
                error:
                   function (XMLHttpRequest, textStatus, errorThrown) {
                    // typically only one of textStatus or errorThrown
                    // will have info
                        //this; // the options for this ajax request
                        pblock.innerHTML = "ERROR: Couldn't connect to namefinder";
                   },
                /* So now we parse the xml file we got from namefinder */
                success: function(xml) {
                  /* Wir müssen nur zur "named" node, da sind die Attribute drinne */
                                    
//                  pblock.innerHTML = pblock.innerHTML + "START <br />";
                  var x=xml.childNodes;
                  var lat;
                  var lon;
                  var name;
                   for (i=0;i<x.length;i++)
                  {
                    pblock.innerHTML = pblock.innerHTML + "<br />" + i + x[i].nodeName;
                       var a = x[i].childNodes;
                       for( p=0; p < a.length; p++)
                       {
                         if( a[p].nodeName == "named" )
                         {
                          //pblock.innerHTML = pblock.innerHTML + "<br />--" + p + a[p].attributes[0].name;
                         // pblock.innerHTML = pblock.innerHTML + "<br />----->" + p + a[p].attributes[0].value

                        
                           
                          lat = a[p].attributes[2].value;
                          lon = a[p].attributes[3].value;
                           
                           /* Damit das erste Ergebniss in der Vorschau angezeigt werden kann */
                           /* The childNode with index 0 is name "#text". I don't know where it comes from and what to do with it */
                           if( p == 1){
                             first_lat = lat;
                             first_lon = lon;
                           }
                           
                           name = a[p].attributes[4].value;
                          // pblock.innerHTML = pblock.innerHTML + "<br /><a href=\"http://www.openstreetmap.org/index.html?lat=" + lat + "&lon=" + lon + "&zoom=10>" + name + "</a>";
                         }

               
                         
                         }
                    //  document.write("Nodename: " + x[i].nodeName);
                  
                   // document.write(" (nodetype: " + x[i].nodeType + ")<br />");
                  
                  }
                  pblock.innerHTML = pblock.innerHTML + "<br />ENDE";
                  
                  /* now generate a url like it would be generated from the export tab of osm and show this in the preview */
                  var lon_min;
                  var lon_max;
                  var lat_min;
                  var lat_max;
                  
                  /* Was calculated for zoomlevel 14 or 16, don't know  */
                  lon_min = first_lon - 0.005;
                  lon_max = first_lon + 0.005;
                  lat_min = first_lat - 0.00747;
                  lat_max = first_lat + 0.00747;
                  
                   var osmurl = "<iframe width=\"425\" height=\"350\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"http://www.openstreetmap.org/export/embed.html?bbox=" + lon_min + "," + lat_min + "," + lon_max + "," + lat_max + "&layer=mapnik\" style=\"border: 1px solid black\"></iframe><br /><small><a href=\"http://www.openstreetmap.org/?lat=50.849999999999994&lon=-0.09999999999999964&zoom=4&layers=B000FTFT\">View Larger Map</a></small>" + lat;

                  
                  pblock.innerHTML = pblock.innerHTML + osmurl;
                 
                  
                }
     })
    
    

  },
  execute: function(loco){
    displayMessage("What to do in the execute function ? Goto OSM I think");
  }
})