2011年8月29日 星期一

2011年8月24日 星期三

Solr Result Grouping

文件及參考:http://wiki.apache.org/solr/FieldCollapsing
需注意:從Solr 3.3、4.0以後才支援。目前release最新版本是3.3。

有空再慢慢看(死)

2011年8月23日 星期二

Using IP Address To Find Country

Using GeoLite Country,參考http://www.maxmind.com/app/csv
  1. 下載GeoLite Country
  2. 建立Table
    CREATE TABLE `geo_country` (
        `start_ip` char(15) NOT NULL COMMENT '起始IP',
        `end_ip` char(15) NOT NULL COMMENT '結束IP',
        `start` bigint(20) NOT NULL COMMENT '起始值',
        `end` bigint(20) NOT NULL COMMENT '結束值',
        `country` char(2) NOT NULL COMMENT '國家簡碼',
        `name` char(50) NOT NULL COMMENT '國家名稱'
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='國家ip'

  3. 將檔名命名成和Table一樣可以在import時自動幫你對應到Table,這邊是geo_country
  4. Import CSV到Mysql
    mysqlimport
     --fields-terminated-by=","
     --fields-optionally-enclosed-by="\""
     --lines-terminated-by="\n"
     --host=<YOUR_HOST>
     --user=<YOUR_USER>
     --password=<YOUR_PASSWORD>
     <YOUR_DATABASE>
     -L /PATH/geo_country.csv
  5. 測試168.95.1.1 計算方式 (168 x 16777216) + (95 x 65536) + (1 x 256) + (1 x 1)
    SELECT * FROM `geo_country` WHERE 2824798465 BETWEEN `start` AND `end`;
若要安裝在Debian上可參考:http://www.php.net/manual/en/geoip.setup.php#93000
輕鬆省事的啦。

另外如果你要求要更精確、夠有錢,也可以不用GeoLite,改用GeoIP。

2011年8月16日 星期二

Tomcat Set URIEncoding

為了讓Tomcat在取得URI的中文參數不會變亂碼,必須做以下處理。

編輯conf/server.xml
<Server port="8005" shutdown="SHUTDOWN">
  ......
  <Service name="Catalina">
    ......

    <Connector port="8080" protocol="HTTP/1.1" URIEncoding="UTF-8"
               connectionTimeout="20000"
               redirectPort="8443" />
    ......

  </Service>
</Server>



官方參考:
http://wiki.apache.org/solr/SolrTomcat#URI_Charset_Config

Arduino Target

http://arduino.tw/

有空...來試著做出這玩意好了。(遠目)
http://www.mandmtoys.com/air-swimmers/clown-fish?zenid=221aff50f3ff0e04fd4f82095f3a125b

Trigger Android Vibrate

http://android.konreu.com/developer-how-to/vibration-examples-for-android-phone-development/

2011年8月11日 星期四

jQueryMobile Note

Version: Beta2

http://taiwansportplace.appspot.com/mobile/search這個頁面中有 bind pagecreate event 在這個頁面中,在此時透過 Ajax 到 API 拉取資料回來,透過 id 塞到一個 ListView。
因為是透過 id 去指定的,所以當網址變成http://taiwansportplace.appspot.com/mobile/search#/mobile/search時,依然有把資料透過 Ajax拉回來。
只是因為這時候會有兩個一樣 page element 在這個 html 中,雖然頁面看起來是空白的,但是其實是有資料塞到第一個 page,只是顯示的是第二個長得一樣的但是沒有資料的 page。

或許會有人想說那就兩個都塞就好了,但是今天在我的另外一個頁面放的是個Google Map。
同時有兩個地圖存在頁面上,我想應該不是好事情,很傷效能。

目前不確定這個是不是我根本違反了jQM的設計精神。
總之在某些連結上透過 data-ajax="false",讓頁面不是用Ajax讀進來。
網址也就不會變成第二個的狀況了。

2011年8月9日 星期二

HTML5 Get User Location

廢話不多說,看Code。
var successCallback = function(position) {
        //前略,map這邊是使用Google Map v3
        //地圖移過去使用者的位置
       map.setZoom(10);
        map.panTo(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
    },
    errorCallback = function() {
        switch(error.code) {
            case error.TIMEOUT:
                alert ('Timeout');
                break;
            case error.POSITION_UNAVAILABLE:
                alert ('Position unavailable');
                break;
            case error.PERMISSION_DENIED:
                alert ('Permission denied');
                break;
            case error.UNKNOWN_ERROR:
                alert ('Unknown error');
                break;
        }
    },
    getLocation = function() {
        //檢查有無支援 HTML5 的geolocation
        if (navigator.geolocation) {
            //會跳出確認是否要讓網頁使用你的位置
            navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
        }
    };
getLocation();  //開始取得位置

用法範例:
navigator.geolocation.getCurrentPosition( successCallback, errorCallback, options );
var watchId = navigator.geolocation.watchPosition( successCallback, errorCallback, options );
navigator.geolocation.clearWatch( watchId );

實作範例:
http://taiwansportplace.appspot.com/mobile/weather

W3C 官方參考:
http://dev.w3.org/geo/api/spec-source.html
http://dev.w3.org/geo/api/spec-source.html#position-options

2011年8月4日 星期四

jQueryMobile Beta2 Released

http://jquerymobile.com/blog/2011/08/03/jquery-mobile-beta-2-released/

摘要:
  1. 支援Nokia系列的60支
  2. 在 Beta1 中出現的「useFastClick」被拿掉了,因為沒辦法在所有平台上面不出錯。
    而且不拿掉會觸發兩次。Orz...
  3. 網址列有時候還是會出現,因為處理連結比較重要。
    會出現是因為踩到 Safari 的 bug XD
    歷程:https://github.com/jquery/jquery-mobile/wiki/iOS-location-bar-hiding-research
  4. Page 的結構不一定要有 data-role="page" 了,範例:http://jsbin.com/ejevis/edit
  5. CheckBox、Radio改變選到的狀態,不會整個反白了。
    Beta 1:http://jquerymobile.com/demos/1.0b1/#/demos/1.0b1/docs/forms/forms-all.html
    Beta 2:http://jquerymobile.com/demos/1.0b2/#/demos/1.0b2/docs/forms/forms-all.html
  6. 有 DOM cache 可以加速有 cache 頁面的讀取速度。
  7. 增加了「create」event,如果動態的加入一些元件到頁面上,則去 trigger 頁面的 create 事件,就會幫你把這個頁面底下的每個元件都呼叫 create 並且畫好它。
    例如:加了一個 ListView到頁面上時,應該呼叫 create。如果只是 ListView 加一個 Row 則呼叫 refresh。
  8. 增加「data-prefetch」在連結上,jQM會幫你做lazy-loading把頁面取回。
  9. 把「autoInitializePage」設為false,可以在 Page 建立前,先做些事情。
  10. loadingMessage 可以隨時更改了。
    如:$.mobile.loadingMessage = "My custom message!";
  11. 如何觸發 swipe 事件的設定可以更改了。
    scrollSupressionThreshold、durationThreshold、horizontalDistanceThreshold、verticalDistanceThreshold
  12. header、footer 的 theme 不會從 page 繼承下來了,不知道是 Bug 還是?
  13. API 文件有每個元件的設定可以看了,不用自己去挖。

2011年8月3日 星期三

iScroll4

http://cubiq.org/dropbox/iscroll4/examples/simple/

Zend Server CE Can't Connect To Local MySQL Server

在Mac上使用Zend Server CE時
要用 command 連 Mysql 時出現
Can't connect to local MySQL server through socket '/tmp/mysql.sock'

解決辦法:
sudo ln -s /usr/local/zend/mysql/tmp/mysql.sock /tmp/mysql.sock

2011年8月1日 星期一

Apache使用Cronolog

參考:http://www.ylmf.net/linux/tips/2010111710026.html

安裝Cronolog:
  1. wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
  2. gzip -d cronolog-1.6.2.tar.gz
  3. tar xf cronolog-1.6.2.tar
  4. cd cronolog-1.6.2
  5. ls
  6. ./configure
  7. make
  8. make install
  9. which cronolog
which cronolog會看到被安裝在 /usr/local/sbin/cronolog
CustomLog "| /usr/local/sbin/cronolog /[PATH_TO_YOUR_LOG]/access-%Y%m%d.log" combined
 <VirtualHost *:80>
  ServerName ......
  DocumentRoot ......
  <Directory /Users/cateyes/work/sites/>
    ......
  </Directory>
  CustomLog "| /usr/local/sbin/cronolog /[PATH_TO_YOUR_LOG]/access-%Y%m%d.log" combined
</VirtualHost>