javascript - 如何获取地理位置并以html形式返回值

标签 javascript html forms geolocation

如何获取地理位置并在单击按钮时将其返回到表单中。

因此,当单击“地理位置”时,会获取我的位置,然后返回输出,以便我稍后可以使用它。 http://jsfiddle.net/Mikki10/as3mg177/1/

地理位置

<html>   
<body>
    <p>Click the button to get your coordinates.</p>
    <button onclick="getLocation()">Get geolocation and put into from</button>
    <p id="demo"></p>
    <script>
        var x = document.getElementById("demo");

        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition);
            } else {
                x.innerHTML = "Geolocation is not supported by this browser.";
            }
        }

        function showPosition(position) {
            x.innerHTML = "Latitude: " + position.coords.latitude +
                "<br>Longitude: " + position.coords.longitude;
        }
    </script>
    <form action="geo/ruteplanlag.php" method="get">
        <input style="width: 98%; margin: 20px auto; text-align: center;" name="startadr" type="text" value="(Custom value like Orlando or geolocation) " />
        <input style="width: 98%; margin: 20px auto; text-align: center;" name="slutadr" type="text" value="Miami" />
        <div class="buttonHolder">
            <input type="submit" value="Start ruteplanlægning">
        </div>
    </form>
</body>
</html>

最佳答案

我现在得到了工作代码。 http://jsfiddle.net/Mikki10/gs5ed5sc/1/

    <script type="text/javascript">
    function getLocationConstant() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(onGeoSuccess, onGeoError);
        } else {
            alert("Your browser or device doesn't support Geolocation");
        }
    }

    // If we have a successful location update
    function onGeoSuccess(event) {
        document.getElementById("Latitude").value = event.coords.latitude;
        document.getElementById("Longitude").value = event.coords.longitude;
        document.getElementById("Position1").value = event.coords.latitude + ", " + event.coords.longitude;

    }

    // If something has gone wrong with the geolocation request
    function onGeoError(event) {
        alert("Error code " + event.code + ". " + event.message);
    }
</script>
<form action="geo/ruteplanlag.php" method="get">
    <div id="divSample" class="hideClass">Latitude:
        <input type="text" id="Latitude" name="Latitude" value="">
        <br>
        <br>Longitude:
        <input type="text" id="Longitude" name="Longitude" value="">
        <br>
    </div>
    <br>Position
    <input type="text" id="Position1" name="Position1" value="Miami">
    <br>
    <br>
    <input type="button" value="Get Location" onclick="getLocationConstant()" />
    <br>
    <br>
    <input type="submit" value="Add GPS Location" class=small>
</form>

关于javascript - 如何获取地理位置并以html形式返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30056002/

相关文章:

javascript - 如果控制台错误不会引起问题,那么 JavaScript 中可以接受这些错误吗?

html - 输入未在媒体查询中内联对齐

jquery - 通过 OpenERP-7 中的特定按钮以只读模式打开表单

javascript - 单击 Bootstrap glyphicon 时,AngularJS 和 jQuery 日历指令可以工作吗?

javascript - 使用node js迭代json数组对象

javascript - 如果其中一个属性包含特定值,则删除数组元素

javascript - 同步 Canvas 绘图/阅读

javascript - 样式化和折叠无序列表

javascript - 使用 Javascript 提交 HTML5 表单并验证其输入

forms - Delphi-辅助表单的OnClose处理程序未调用