網(wǎng)頁中自動獲取經(jīng)緯度值并在地圖中顯示當前位置實例代碼

下面的HTML代碼就是實例完整代碼,如果你需要本項目完整實例代碼,下載鏈接在文章最底部

網(wǎng)頁中自動獲取經(jīng)緯度值并在地圖中顯示當前位置,可在地圖中進行拖動選擇當前位置并輸出經(jīng)緯度,這是項目實例代碼,可參考用于你的項目中。

微信圖片_20201025093358.jpg

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>經(jīng)緯度獲取示例代碼-520YM.NET</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    <meta content="yes" name="apple-mobile-web-app-capable" />
    <meta content="yes" name="apple-touch-fullscreen" />
    <meta content="telephone=no,email=no" name="format-detection" />
    <meta
      name="App-Config"
      content="fullscreen=yes,useHistoryState=yes,transition=yes"
    />
    <link rel="stylesheet" type="text/css" href="static/css/scs.min.css" />
    <link rel="stylesheet" type="text/css" href="static/css/main.css" />
    <style type="text/css">
      #mapcontainer {
        width: 100%;
        height: 300px;
      }
      a[title="到百度地圖查看此區(qū)域"] {
        display: none;
      }
      span[_cid="1"] {
        display: none;
      }
    </style>
  </head>
  <body>

  
    <section>
      <div class="main cc">
        <h3 class="indextt1">獲取當前位置經(jīng)緯度并在地圖中顯示</h3>
        <ul class="nytxt1 cc">
          <form enctype="multipart/form-data">
            <li>
              <input
                class="nytxt1input1"
                id="coordinate"
                name="info[coordinate]"
                type="text"
                readonly=""
                value="位置經(jīng)緯度:"
              />
            </li>
            <li>
              <div>請在下方地圖選擇您的位置</div>
              <div id="mapcontainer"></div>
            </li>
            <li>
              <input
                class="nytxt1btn1"
                id="btnsumttj"
                name="dosave"
                type="button"
                onclick="check_message()"
                value="集源碼"
              />
            </li>
          </form>
        </ul>
      </div>
    </section>
    <script src="static/js/public.js"></script><!--這個js是用于頁面樣式用途,無關(guān)緊要-->
    <script src="http://api.map.baidu.com/api?v=3.0&amp;ak=這里填寫你的百度地圖ak密鑰"></script>
    <script>
      var map = new BMap.Map("mapcontainer");
      var point = new BMap.Point(116.404, 39.915);
      map.centerAndZoom(point, 15);
      var geolocation = new BMap.Geolocation();
      var marker = new BMap.Marker(new BMap.Point(116.404, 39.915));
      map.addOverlay(marker);
      marker.getIcon().setSize({ width: 30, height: 40 });
      var selectedPosition = point;
      geolocation.getCurrentPosition(
        function (r) {
          if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            //- var mk = new BMap.Marker(r.point);
            //- map.addOverlay(mk);
            marker.setPosition(r.point);
            map.panTo(r.point);
            document.getElementById("coordinate").value =
              "位置經(jīng)緯度:" + r.point.lng + "," + r.point.lat;
            // alert('您的位置:'+r.point.lng+','+r.point.lat);
            selectedPosition = r.point;
          } else {
            // alert('failed'+this.getStatus());
          }
        },
        { enableHighAccuracy: true }
      );
      function showInfo(e) {
        // alert(e.point.lng + ", " + e.point.lat);
        marker.setPosition(e.point);
        selectedPosition = e.point;
        document.getElementById("coordinate").value =
          "位置經(jīng)緯度:" + e.point.lng + "," + e.point.lat;
      }
      map.addEventListener("touchend", showInfo);
      map.addEventListener("touchmove", function (e) {
        map.enableDragging();
      });
      map.addEventListener("touchend", function (e) {
        map.disableDragging();
      });
    </script>
  </body>
</html>

完整項目實例代碼下載

https://www.520ym.net/xuexi/anquan/7076.html

DEMO演示

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容