【问题标题】:Obtain google maps coordinates from user从用户获取谷歌地图坐标
【发布时间】:2016-03-20 18:22:25
【问题描述】:

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

我希望用户填写表格,一旦他去现场

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

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

刚刚在 T3H 完成了第一门 JS 基础课程,我完全迷失了方向。与此相比,HTML 和 CSS 就像在公园里散步,所以我需要一些指导。

【问题讨论】:

标签: javascript google-maps


【解决方案1】:

获取用户坐标的最简单方法是使用 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>

【讨论】:

  • 抱歉回复晚了,您的解决方案如我所愿,谢谢!
  • 我把它放在了codepen中,在桌面上可以正常工作,但在移动设备上就不行,知道为什么吗?
  • navigator.geolocation 是否在移动设备上启用?
  • scaisEdge,我的手机上没有。我应该自己检查一下,不要浪费你的时间,非常感谢你的回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
相关资源
最近更新 更多