简体   繁体   English

如何获取我当前位置的经度和纬度并显示在两个单独的 HTML 表格上

[英]How to get longitude and latitude of my current location and display on two separate HTML form

Can anyone help with getting my current longitude and latitude on-click of a button, and display on two separate input form in HTML.任何人都可以通过单击按钮来帮助获取我当前的经度和纬度,并在 HTML 中的两个单独的输入表单上显示。 Below is what my form will be like and on click of the "Get" button it will get my current LNG and LAT then display on forms 1 and 2.下面是我的表单的样子,点击“获取”按钮,它将获得我当前的 LNG 和 LAT,然后显示在 forms 1 和 2 上。




<form>  


<button onclick="onFormSubmit()">GET CURRENT LOCATION</button>


<script>
function onGeoSuccess (position) {
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;

        // NEXT 2 LINES WILL SET VALUE OF RESPECTIVE INPUTS
        document.getElementById('lat').value = lat;
        document.getElementById('lng').value = long;

        // MAKE API CALL HERE, OR ANY OTHER NEXT STEPS
    }

    function onGeoError (err) {
        console.error("Error while trying to get position", err);
    }

    function onFormSubmit () {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(onGeoSuccess, onGeoError);
        } else {
            alert('GeoLocation not supported or not allowed');
        }
    }</script>

              <div class="form-group">
                <input type="text" name="lat" class="form-control" id="lat" placeholder="Your Latitude" data-msg="Please enter your latitude">
                <div class="validate"></div>
              </div>

<div class="form-group">
                <input type="text" name="lng" class="form-control" id="lng" placeholder="Your Longitude" data-msg="Please enter your Longitude">
                <div class="validate"></div>
              </div>

              <div class="form-group">
                <input type="text" name="subject" class="form-control" id="contact-subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject">
                <div class="validate"></div>
              </div>

</form>



This can be done using some simple JavaScript:这可以使用一些简单的 JavaScript 来完成:

/* Add an onsubmit method to your form */
<form onsubmit="onFormSubmit()">
/*...*/
function onGeoSuccess (position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;

    // NEXT 2 LINES WILL SET VALUE OF RESPECTIVE INPUTS
    document.getElementById('lat').value = lat;
    document.getElementById('lng').value = long;

    // MAKE API CALL HERE, OR ANY OTHER NEXT STEPS
}

function onGeoError (err) {
    console.error("Error while trying to get position", err);
}

function onFormSubmit () {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(onGeoSuccess, onGeoError);
    } else {
        alert('GeoLocation not supported or not allowed');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在OpenLayers中获取用户的当前位置以及纬度和经度 - how to get the current location of the user along with latitude and longitude in OpenLayers 如何获取当前位置(经度和纬度)并将其发送到反向地理编码API? - How to get current location (latitude and longitude) and send it to an reverse geocoding API? 检测到当前位置后如何在mapbox中获取经度和纬度? - How to get longitude and latitude in mapbox after detect current location? 如何使用javascript显示离我所在地点最近的纬度和经度? - How to display the nearest latitude and longitude to my location using javascript? 如何使用html在php中发布我所在位置的经度和纬度? - How to post the latitude and longitude of my location in php using html? 如何获得两个位置之间路线的中心纬度和经度 - how to get center latitude and longitude in route between two location 如何从html表单到谷歌地图获取纬度经度值 - How to get latitude longitude value from html form to google map 查找当前位置的纬度和经度 - Find latitude and longitude of current location 计算当前位置经纬度 - Calculate the current location longitude and latitude 如何使用谷歌地图查找当前位置(经度和纬度)? - How to find the current location(longitude and latitude) using Google Map?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM