-1

私は次のコードを持っています:

<script type = "text/javascript">
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(geofields);
        }
    }

    function geofields() {
        if (document.getElementById('starthere').checked == 1) {
            document.getElementById('start').value = position.coords.latitude + " " + position.coords.longitude;
        } else {
            document.getElementById('start').value = '';
        }
    }
</script>

function geofields(){'start'要素の値を(GetLocation()スクリプトから受け取った)座標に変更してGeoLocationを表示したいのですが、上記のコードでは機能しません。助言がありますか?

4

3 に答える 3

1

positionパラメータを追加しgeofieldsて呼び出すgetLocation(またはハンドラとして設定する)必要があります。

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(geofields);
    }
}

function geofields(position) {
    if (document.getElementById('starthere').checked == 1) {
        document.getElementById('start').value = position.coords.latitude + " " + position.coords.longitude;
    } else {
        document.getElementById('start').value = '';
    }
}

document.addEventListener('load', getLocation);
于 2012-12-14T19:52:30.660 に答える
0

body要素のonloadプロパティに関数呼び出しを配置し​​てみてください。

<body onload="getLocation();">

これにより、関数の実行時にdomが確実に読み込まれるようになります。

よろしく。

于 2012-12-14T19:49:04.033 に答える
0

MDNドキュメントから:

navigator.geolocation.getCurrentPosition(function(position) {
  do_something(position.coords.latitude, position.coords.longitude);
});

position関数にパラメータを渡していないようですgeofields

于 2012-12-14T19:53:07.623 に答える