FRS/gmap/code

From MinecraftOnline

Jump to: navigation, search

This is code that can be used in Bookmarklets to display the rail lines on the Map. The code is written in CoffeeScript, and has to be compiled to javascript first. The data for the rail lines can be found on FRS/gmap.

# Yucky code to make sure geometry is loaded
delete new google.maps.Polyline

dashSymbol =
    path: 'M 0,-1 0,1'
    strokeColor: 'black'
    strokeWeight: 5
    scale: 3

handleData = (data) ->
    for name, info of data
        poly = new google.maps.Polyline
            strokeColor: info.color
            strokeWeight: 5
            path: google.maps.geometry.encoding.decodePath info.path
            map: overviewer.map
            icons: [
                icon: dashSymbol
                offset: '0'
                repeat: '20px'
            ]
        for bname, binfo of info.branches
            bpoly = new google.maps.Polyline
                strokeColor: binfo.color
                strokeWeight: 4
                path: google.maps.geometry.encoding.decodePath binfo.path
                map: overviewer.map
        for bname, binfo of info.stations
            marker = new google.maps.Marker
                position:new google.maps.LatLng binfo.lat, binfo.lng
                icon:"/w/images/c/c5/Mapmarker_settlementicon.png"
                map: overviewer.map
                title:binfo.title
    return
xhr = new XMLHttpRequest()
xhr.onreadystatechange = ->
    if xhr.readyState == 4 && xhr.status == 200
        response = JSON.parse xhr.responseText
        for _, page of response.query.pages
            data = JSON.parse page.revisions[0]['*']
            handleData data
    return

xhr.open "GET", "/w/api.php?format=json&action=query&titles=FRS/gmap&prop=revisions&rvprop=content"
xhr.send null

And here's the version that can be used directly as bookmarklet:

javascript:(function(){var e,o,t;delete new google.maps.Polyline,e={path:"M 0,-1 0,1",strokeColor:"black",strokeWeight:5,scale:3},o=function(o){var t,a,n,r,s,i,p,l,g;for(i in o){r=o[i],p=new google.maps.Polyline({strokeColor:r.color,strokeWeight:5,path:google.maps.geometry.encoding.decodePath(r.path),map:overviewer.map,icons:[{icon:e,offset:"0",repeat:"20px"}]}),l=r.branches;for(a in l)t=l[a],n=new google.maps.Polyline({strokeColor:t.color,strokeWeight:4,path:google.maps.geometry.encoding.decodePath(t.path),map:overviewer.map});g=r.stations;for(a in g)t=g[a],s=new google.maps.Marker({position:new google.maps.LatLng(t.lat,t.lng),icon:"/w/images/c/c5/Mapmarker_settlementicon.png",map:overviewer.map,title:t.title})}},t=new XMLHttpRequest,t.onreadystatechange=function(){var e,a,n,r,s;if(4===t.readyState&&200===t.status){n=JSON.parse(t.responseText),s=n.query.pages;for(r in s)a=s[r],e=JSON.parse(a.revisions[0]["*"]),o(e)}},t.open("GET","/w/api.php?format=json&action=query&titles=FRS/gmap&prop=revisions&rvprop=content"),t.send(null)})();

And a version with station labels and small markers.

javascript:(function(){var e,o,t;delete new google.maps.Polyline,o={path:"M 0,-1 0,1",strokeColor:"black",strokeWeight:5,scale:3},e={path:"M 0,-1 0,1",strokeColor:"black",strokeWeight:3,scale:2},t=function(t){var a,l,r,n,i,s,p,g,c,m;for(p in t){n=t[p],g=new google.maps.Polyline({strokeColor:n.color,strokeWeight:5,clickable:!1,path:google.maps.geometry.encoding.decodePath(n.path),map:overviewer.map,icons:[{icon:o,offset:"0",repeat:"20px"}]}),c=n.branches;for(l in c)a=c[l],r=new google.maps.Polyline({strokeColor:a.color,strokeWeight:3,clickable:!1,path:google.maps.geometry.encoding.decodePath(a.path),map:overviewer.map,icons:[{icon:e,offset:"0",repeat:"15px"}]});m=n.stations;for(l in m)a=m[l],s=new google.maps.Marker({position:new google.maps.LatLng(a.lat,a.lng),icon:{path:google.maps.SymbolPath.CIRCLE,strokeColor:"black",strokeWeight:2,fillColor:"orangered",fillOpacity:.8,scale:4},map:overviewer.map,title:a.title}),i=new MapLabel({text:a.title,position:new google.maps.LatLng(a.lat,a.lng),fontColor:"white",fontSize:10,align:"left",strokeColor:"black"}),i.bindTo("map",s),i.bindTo("position",s),i.bindTo("zIndex",s)}},jQuery.getScript("http://google-maps-utility-library-v3.googlecode.com/svn/trunk/maplabel/src/maplabel-compiled.js",function(){var e;return e=new XMLHttpRequest,e.onreadystatechange=function(){var o,a,l,r,n;if(4===e.readyState&&200===e.status){l=JSON.parse(e.responseText),n=l.query.pages;for(r in n)a=n[r],o=JSON.parse(a.revisions[0]["*"]),t(o)}},e.open("GET","/w/api.php?format=json&action=query&titles=FRS/gmap&prop=revisions&rvprop=content"),e.send(null)})})();

Since this code is placed on this wiki, it's licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported license (see MinecraftOnline:Copyrights for details).


Personal tools