FRS/gmap/code

From MinecraftOnline
Jump to navigation Jump to 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.

require = (pkg) =>
    @_required ?= []
    if pkg in @_required
        Promise.resolve pkg
    else
        new Promise (resolve, reject) =>
            script = document.createElement 'script'
            script.src = "//unpkg.com/#{pkg}"
            script.onload = =>
                @_required.push pkg
                resolve pkg
                return
            script.onerror = reject
            document.body.appendChild script
            return

handleData = (data) =>
    for name, info of data
        L.Polyline.fromEncoded info.path, color: 'black'
                .bindTooltip name, sticky: true
                .addTo overviewer.map
        L.Polyline.fromEncoded info.path, color: info.color, dashArray: '5 10'
                .bindTooltip name, sticky: true
                .addTo overviewer.map
        for bname, binfo of info.branches
            L.Polyline.fromEncoded binfo.path, color: binfo.color
                    .bindTooltip bname, sticky: true
                    .addTo overviewer.map
        for _, station of info.stations
            L.circleMarker [station.lat, station.lng],
                    radius: 4
                    color: 'black'
                    weight: 2
                    fillColor: 'orangered'
                    fillOpacity: 0.8
                    pane: 'markerPane'
                .bindTooltip station.title
                .addTo overviewer.map
    return

require "polyline-encoded"
    .then =>
        fetch "/w/api.php?format=json&action=query&titles=FRS/gmap&prop=revisions&rvprop=content"
    .then (response) =>
        response.json()
    .then (json) =>
        for _, page of json.query.pages
            handleData JSON.parse page.revisions[0]['*']
        return

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

javascript:(function(){var o,e=[].indexOf;o=(o=>{var e,r,i,n,a,l,t,d;for(a in o){for(i in n=o[a],L.Polyline.fromEncoded(n.path,{color:"black"}).bindTooltip(a,{sticky:!0}).addTo(overviewer.map),L.Polyline.fromEncoded(n.path,{color:n.color,dashArray:"5 10"}).bindTooltip(a,{sticky:!0}).addTo(overviewer.map),l=n.branches)r=l[i],L.Polyline.fromEncoded(r.path,{color:r.color}).bindTooltip(i,{sticky:!0}).addTo(overviewer.map);for(e in t=n.stations)d=t[e],L.circleMarker([d.lat,d.lng],{radius:4,color:"black",weight:2,fillColor:"orangered",fillOpacity:.8,pane:"markerPane"}).bindTooltip(d.title).addTo(overviewer.map)}}),(o=>(null==this._required&&(this._required=[]),e.call(this._required,o)>=0?Promise.resolve(o):new Promise((e,r)=>{var i;(i=document.createElement("script")).src=`//unpkg.com/${o}`,i.onload=(()=>{this._required.push(o),e(o)}),i.onerror=r,document.body.appendChild(i)})))("polyline-encoded").then(()=>fetch("/w/api.php?format=json&action=query&titles=FRS/gmap&prop=revisions&rvprop=content")).then(o=>o.json()).then(e=>{var r,i,n;for(r in n=e.query.pages)i=n[r],o(JSON.parse(i.revisions[0]["*"]))})}).call(this);

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).