javascript - 从用户处获取谷歌地图坐标

标签 javascript google-maps

我什至不确定这是否可能,但它就是这样。

我希望用户填写表单,一旦他进入该字段

<label for="pos">My possition</label>          
<input type="email" id="pos" name="clients_position">

我想请求他的位置并从他那里接收格式为“40.417035, -3.685389”的 Google map 坐标,理想情况下我希望它们自动添加到输入字段中。

刚刚在 T3H 学习了第一门 JS 基础类(class),我完全迷失了。与此相比,HTML 和 CSS 简直就是在公园散步,所以我需要一些指导。

最佳答案

获取用户坐标的最简单方法是使用 html 5 地理定位,这是一个示例

    <!DOCTYPE html>
    <html>
      <body>

      <p>Click the button to get  the coordinates.</p>

      <button onclick="getLocation()">Get coordinates </button>

      <p id="my_sample"></p>

      <script>
      var x = document.getElementById("my_sample");

      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>

      </body>
    </html>

关于javascript - 从用户处获取谷歌地图坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36117713/

相关文章:

javascript - 用于受密码保护的网站的 nodejs 网络抓取工具

angular - 由于错误 : No provider for MapsAPILoader?,Google map 无法加载到 Angular 项目中

javascript - 为什么从 Google map 版本 2 切换到版本 3 后我的标记不显示?

android - 在android中两个位置之间的 map 上查找路线

javascript - 使用谷歌闭包创建事件

javascript - 当位置从绝对位置更改为固定位置时,如何使元素保持在原位

javascript - 为什么这个 emberjs 模板不更新?

javascript - 在 JQgrid 中添加新记录后过滤行

javascript - 谷歌地图API搜索特定国家的邮政编码

android - 如何为我的 Android 应用程序获取 Google Places API key