Source Code

版本:
  • 0.3 beta

更新記錄:
  • 2011.9.21 - 基本隨機菜單功能。
  • 2011.9.22 - 加入飲料分類,解除分類項目限制。
  • 2011.9.23 - 變更分類排序提升效率。
  • 2011.9.24 - 開放原始碼、加入Google Map功能。
資料程式
<script type="text/javascript">
//試算表Jsonp資料
var spreadsheets = "https://spreadsheets.google.com/feeds/list/0AsHdyDgIiLuHdDlHNzN4WS1RNy1NdmVCLWEzQjgyZ3c/od6/public/basic?hl=zh_TW";
//試算表編輯連結
document.getElementById('editmenu').href="https://docs.google.com/spreadsheet/ccc?key=0AsHdyDgIiLuHdDlHNzN4WS1RNy1NdmVCLWEzQjgyZ3c&hl=zh_TW";
</script>
核心程式:
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAKkwXcNnNNu5jn0_cfHg8bBTjyUjLsXKCut9D87oDOn6GomoIgxQRd6eVYzcQ5s3v5LCW4a0U9uhWfw" type="text/javascript"></script>
<script type="text/javascript">
//Google地圖搜索
function googlemap(address) {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        var center = new GLatLng();
        geocoder = new GClientGeocoder();
        var marker = new GMarker(center, {draggable: true});
        map.addOverlay(marker);
        if (geocoder) {
            geocoder.getLatLng(
                address,
                function(center) {
                    if (!center) {
                        alert(address + "not found");
                    } else {
                        map.clearOverlays()
                        map.setCenter(center, 16);
                        var marker = new GMarker(center, {draggable: true});
                        map.addOverlay(marker);
                    }
                }
            );
        }
    }
}
//美食搜索
function food(json) {
    var menu = json.feed.entry;
    var urlformat = /^(https?:\/\/)[a-zA-Z0-9_\-.:#/~}?]+$/;
    var longbreak = 0;
    var item = "";
    var p1 = 0;
    var p2 = 0;
    var other = '';
    var timestep = document.getElementsByName('timestep');
    //取得時段
    for(var i=0;i<timestep.length;i++)
        if(timestep[i].checked)
            var step = timestep[i].value;
    //搜尋迴圈
    while(longbreak<menu.length*10) {
        var r = Math.round(Math.random()*(json.feed.entry.length-1));
        var title = menu[r].title.$t; //時段類別字串
        //時段比對
        if(title.indexOf(step)!=-1)
            break;
        longbreak++;
    }
    var content = menu[r].content.$t; //其它字串
    for(var i=0;i<10;i++) {
        p2 = content.indexOf(", ",p1);
        if(p2!=-1)
            item = content.substring(p1, p2); //各段資料
        else
            item = content.substring(p1, content.length); //尾段資料
        var i_title = item.substring(0, item.indexOf(": "));
        var i_content = item.substring(item.indexOf(": ")+2, item.length);
        if(urlformat.test(i_content)) //超連結轉換
            i_content = "<a target=\"_blank\" href=\"" + i_content + "\">" + i_content + "</a>";
        if(i_title == "店家地址") {
            var address = i_content;
            i_content = i_content + "<br /><div align=\"center\" id=\"map\" style=\"width: 300px; height: 200px\"></div>";
        }
        other += i_title + ":" + i_content + "<br />"; //資料合併
        p1 = p2 + 2;
        if(p2==-1)
            break;
    }
    document.getElementById("showmenu").innerHTML = other;
    googlemap(address);
}
//取得資料
function getFood() {
    var removeScript = document.getElementById('jsonScript');
    if(removeScript) //移除舊腳本
        removeScript.parentNode.removeChild(removeScript);
    var script = document.createElement('script'); //建立新腳本
    script.type = 'text/javascript';
    script.id = 'jsonScript';
    script.src = spreadsheets + "&alt=json-in-script&callback=food";
    document.getElementsByTagName('head')[0].appendChild(script);
    document.getElementById("showmenu").innerHTML = '<img src=\"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsrxjxX_5DoCeF_sNvwVWGS3H5aiOZtwQyyvcAgo1UzI9LpcPeZa9oaHVCCru_brzCcOeGhgpDe8aGNTQ-lZCV4_X9FMd5dgrjMp8ByCCGHZTVzLcIGUHQDbWYm_8UDhzPYzhw6us4zGUx/\" />';
}
</script>