位置情報(GPS)の利用

位置情報(Geolocation)を取得できます。

droid.getCurrentPosition(callback_ok, callback_ng, options) 一回だけ位置情報取得
droid.watchPosition(callback_ok, callback_ng, options) 継続的に位置情報を取得
droid.clearWatchPosition(watchId) 位置情報の取得をキャンセル

HTML5のGeolocation APIと互換性を持たせてあります。

※本来、Android1.6では、Geolocation APIは利用できませんが、jsWaffleを使うことにより、Geolocation APIが使えるようになります。

以下、継続的に位置情報を取得する方法です

<!DOCTYPE html>  
<html><head><meta charset="utf-8">
<script type="text/javascript" src="jsWaffle.js"></script>
<script>
    var s = "";
    droid.watchPosition(
        function (position){
            // オブジェクトから値を取り出す
            var lat = position.coords.latitude;  // 緯度
            var lon = position.coords.longitude; // 経度
            var acc = position.coords.accuracy;  // 精度(m)
            var alt = position.coords.altitude;  // 標高
            // 
            var url = "geo:" + lat + "," + lon + "," + acc;
            var msg = "緯度:" + lat + ",経度:" + lon + ",精度:" + acc + "m";
            s += "<a href='" + url + "'>" + msg + "</a><br/>";
            $("info").innerHTML = s;
        },
        function(err){
            alert("失敗:" + err);
        });
</script>
</head>
<body>
    <div id="info"></div>
</body>
</html>

以下、一度だけ位置情報を取得する利用例です。

<h2>Geolocation</h2>
<div class="buttons">
    <button  id="bntLocation" onclick="getLoc()">Get Location</button>
    <button  onclick="cancelLoc()">Stop Location</button>
    <button  id="bntLocation2" onclick="getLoc2()">Get Location&Show Map</button>
    <script>
        var watchid = 0;
        var pos_options = {
            enableHighAccuracy: true,
            timeout: 3000,
            maximumAge: 3000
        };
        
        function getLoc() {
            $("bntLocation").disabled = true;
            // jsWaffle original method
            watchid = droid.getCurrentPosition(
                showLocation,
                errorLocation,
                pos_options);
            /*
            // HTML5 geolocation method
            navigator.geolocation.getCurrentPosition(
                showLocation,
                errorLocation,
                pos_options);
            */
        }
        // Show Position Info
        function showLocation(position) {
            $("bntLocation").disabled = false;
            var lat = position.coords.latitude;
            var lon = position.coords.longitude;
            var acc = position.coords.accuracy ;
             droid.vibrate(500);
            alert("geo:" + lat + "," + lon + "," + acc);
        }
        function errorLocation (errorObj) {
            var message = errorObj.message;
            var code = errorObj.code;
            $("bntLocation").disabled = false;
            alert("location error : " + code + " : " + message);
        }
        function cancelLoc() {
            // droid.clearWatchPosition(watchid);
            navigator.geolocation.clearWatch(watchid);
            $("bntLocation").disabled = false;
            $("bntLocation2").disabled = false;
        }
        function getLoc2() {
            $("bntLocation2").disabled = true;
            watchid = droid.getCurrentPosition(
                showLocation2,
                errorLocation,
                pos_options);
        }
        function showLocation2(position){
            $("bntLocation").disabled = false;
            var lat = position.coords.latitude;
            var lon = position.coords.longitude;
            var acc = position.coords.accuracy ;
             droid.vibrate(500);
            droid.startIntent("geo:" + lat + "," + lon + "," + acc);
            $("bntLocation2").disabled = false;
        }
    </script>
</div>