0

W3C 地理位置情報 API を実装しようとしていますが、自分の場所に関するブラウザーのアクセス許可を与えているにもかかわらず、Firefox から使用しているときにエラー 2 (位置を利用できません) が発生し、Chrome から使用しようとするとエラー 1 (アクセスが拒否されました) が発生します。以下は私のコードです:

JS 関数を呼び出す HTML ボタン:

次に、Geolocation API について説明します。

<input type="button" name="canbutt" value="draw c" onClick="javascript:geo()"/>

そして私のJSコード:

         function geo(){

                    if (navigator.geolocation) {

                            navigator.geolocation.getCurrentPosition(
                                displayPosition, 
                                displayError);
                    }
                else {
                        alert("Geolocation is not supported by this browser");
                    }

        function displayPosition(position) {
                alert("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude);
                        }
        function displayError(error) {
                var errors = { 
                    1: 'Permission denied',
                    2: 'Position unavailable',
                    3: 'Request timeout'
                    };
                alert("Error: " + errors[error.code]);
                    }


    }

     function canvas() {
                var canvas = document.getElementById("canv");
                var context = canvas.getContext("2d");
                context.fillStyle="red";
                context.strokeStyle="blue";
                context.strokeRect(50,25,50,60);
                context.fillRect(50,25,50,60);
                context.strokeStyle="green";
                context.moveTo(0,0);
                context.lineTo(300,150);
                context.stroke();
                context.fillStyle="blue";
                context.font="bold 12px Arial";
                context.textAlign="start";
                context.fillText("This is some Text",50,110);
                var gradient=context.createLinearGradient(0,0,100,100);
                gradient.addColorStop(0,"white");
                gradient.addColorStop(1,"black");
                context.fillStyle=gradient;
                context.fillRect(50,50,100,100);
                context.fillText("Im using Gradient!",60,10);
                context.drawImage(image, 20,20);


                    };
4

1 に答える 1

1

から Geolocation を使用するには制限がありますfile:/// URI。ページを Web サーバーにアップロードしてみてください (ローカル サーバーも機能します)。

于 2012-04-03T11:37:49.350 に答える