亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue中實現(xiàn)高德定位功能

 更新時間:2019年12月03日 10:17:29   作者:寂寞花如雪  
這篇文章主要介紹了vue中實現(xiàn)高德定位功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

一、獲取key及在index.htm中引入

首先需要成為高德開發(fā)者,申請到適合項目的key.并在index.html中進行引入

<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.3&key=d79ff396531b948ce14d5be1c733fc36"></script>

二、在配置文件中進行相應配置

根據(jù)vue腳手架的不用需要在不同的文件中進行配置。

我項目使用的是cli3的腳手架,需要在Vue.config.js中進行高德地圖配置

externals: {
  'AMap': 'AMap' // 高德地圖配置
 }

三、在需要用到的地方進行地圖初始化及定位操作

因項目需求只需要在注冊頁面進行默認定位,故只引用一次就行。并沒有單獨的抽離出來,可以根據(jù)項目的需求進行抽離。

import AMap from "AMap"; // 引入高德地圖

data() {
  // debugger
  return {
    locationData: {
     // 用于定位相關信息提交
     lat: "", // 緯度
    lon: "", // 經(jīng)度
     province: "", // 省
     city: "", // 市
     district: "", // 區(qū) 縣
     nowPlace: "", // 省-市-區(qū)
     Address: "" // 詳細地址
    }
  };
 },
methods:{
 getLocation() {
   const self = this;
   AMap.plugin("AMap.Geolocation", function() {
    var geolocation = new AMap.Geolocation({
     enableHighAccuracy: true, // 是否使用高精度定位,默認:true
     timeout: 10000, // 超過10秒后停止定位,默認:無窮大
     maximumAge: 0, // 定位結(jié)果緩存0毫秒,默認:0
     convert: true // 自動偏移坐標,偏移后的坐標為高德坐標,默認:true
    });

    geolocation.getCurrentPosition();
    AMap.event.addListener(geolocation, "complete", onComplete);
    AMap.event.addListener(geolocation, "error", onError);

    function onComplete(data) {
     // data是具體的定位信息
     // debugger
     console.log("定位成功信息:", data);
     self.newGetAddress(data.position.lat, data.position.lng);
    }

    function onError(data) {
     // debugger
     // 定位出錯
     console.log("定位失敗錯誤:", data);
     self.getLngLatLocation();
    }
   });
  },
  getLngLatLocation() {
   const self = this;
   AMap.plugin("AMap.CitySearch", function() {
    var citySearch = new AMap.CitySearch();
    citySearch.getLocalCity(function(status, result) {
     if (status === "complete" && result.info === "OK") {
      // 查詢成功,result即為當前所在城市信息
      console.log("通過ip獲取當前城市:", result);
      //逆向地理編碼
      AMap.plugin("AMap.Geocoder", function() {
       var geocoder = new AMap.Geocoder({
        // city 指定進行編碼查詢的城市,支持傳入城市名、adcode 和 citycode
        city: result.adcode
       });

       var lnglat = result.rectangle.split(";")[0].split(",");

       geocoder.getAddress(lnglat, function(status, data) {
        if (status === "complete" && data.info === "OK") {
         // result為對應的地理位置詳細信息
         console.log(data);
         self.userInfo.ProvinceName = data.regeocode.addressComponent.province.toString();
         self.userInfo.CCityName =
          data.regeocode.addressComponent.city;
         self.userInfo.RegionName =
          data.regeocode.addressComponent.district;
        }
       });
      });
     }
    });
   });
  },
  newGetAddress: function(latitude, longitude) {
   const _thisSelf = this;
   _thisSelf.locationData.lat = latitude;
   _thisSelf.locationData.lon = longitude;
   const mapObj = new AMap.Map("mapAmap1");
   mapObj.plugin("AMap.Geocoder", function() {
    const geocoder = new AMap.Geocoder({
     city: "全國", // city 指定進行編碼查詢的城市,支持傳入城市名、adcode 和 citycode
     radius: 100 // 以已知坐標為中心點,radius為半徑,返回范圍內(nèi)興趣點和道路信息
    });
    const lnglat = [longitude, latitude]; // 倒序反寫經(jīng)緯度
    // 天津120 北京110 上海310 重慶500 ,
    const reg1 = /^[1][1][0][0-9][0-9][0-9]/;
    const reg2 = /^[1][2][0][0-9][0-9][0-9]/;
    const reg3 = /^[5][0][0][0-9][0-9][0-9]/;
    const reg4 = /^[3][1][0][0-9][0-9][0-9]/;
    geocoder.getAddress(lnglat, function(status, result) {
     console.log("getAddress", result);
     if (status === "complete" && result.info === "OK") {
      // result為對應的地理位置詳細信息
      const adcode = result.regeocode.addressComponent.adcode; // 省市編碼
      if (
       reg1.test(adcode) ||
       reg2.test(adcode) ||
       reg3.test(adcode) ||
       reg4.test(adcode)
      ) {
       _thisSelf.locationData.city =
        result.regeocode.addressComponent.province;
      } else {
       _thisSelf.locationData.city =
        result.regeocode.addressComponent.city;
      }
      // 提取 省 市 區(qū) 詳細地址信息 重拼裝省-市-區(qū)信息
      _thisSelf.locationData.province =
       result.regeocode.addressComponent.province;
      _thisSelf.locationData.district =
       result.regeocode.addressComponent.district;
      _thisSelf.locationData.Address = result.regeocode.formattedAddress;
      _thisSelf.locationData.nowPlace =
       result.regeocode.addressComponent.province +
       result.regeocode.addressComponent.city +
       result.regeocode.addressComponent.district;
      _thisSelf.userInfo.ProvinceName = _thisSelf.locationData.province;
      _thisSelf.userInfo.CCityName = _thisSelf.locationData.city;
      _thisSelf.userInfo.RegionName = _thisSelf.locationData.district;
     } else {
      console.log(_thisSelf.locationData); // 回調(diào)函數(shù)
     }
    });
   });
  }
},
 created() {
  this.getLocation();
 }

至此整個定位的實現(xiàn)全部結(jié)束,可以準確的獲取到當前所在的省市區(qū)。

總結(jié)

以上所述是小編給大家介紹的vue中實現(xiàn)高德定位功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關文章

  • vue 中swiper的使用教程

    vue 中swiper的使用教程

    本文通過實例代碼給大家介紹了vue 中swiper的使用,感興趣的朋友跟隨腳本之家小編一起學習吧
    2018-05-05
  • vue實現(xiàn)點擊按鈕倒計時

    vue實現(xiàn)點擊按鈕倒計時

    這篇文章主要為大家詳細介紹了vue實現(xiàn)點擊按鈕倒計時,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • vue中element-ui組件默認css樣式修改的四種方式

    vue中element-ui組件默認css樣式修改的四種方式

    在前端項目中會運用各種組件,有時組件的默認樣式并不是你項目中所需要的,你需要更改樣式,下面這篇文章主要給大家介紹了關于vue中element-ui組件默認css樣式修改的四種方式,需要的朋友可以參考下
    2021-10-10
  • 詳解vue axios中文文檔

    詳解vue axios中文文檔

    本篇文章主要介紹了詳解axios中文文檔,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Element 的 el-table 表格實現(xiàn)單元格合并功能

    Element 的 el-table 表格實現(xiàn)單元格合并功能

    這篇文章主要介紹了Element 的 el-table 表格實現(xiàn)單元格合并功能,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2024-07-07
  • Vue項目通過network的ip地址訪問注意事項及說明

    Vue項目通過network的ip地址訪問注意事項及說明

    這篇文章主要介紹了Vue項目通過network的ip地址訪問注意事項及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue使用計算屬性完成動態(tài)滑竿條制作

    vue使用計算屬性完成動態(tài)滑竿條制作

    這篇文章主要介紹了vue使用計算屬性完成動態(tài)滑竿條制作,文章圍繞計vue算屬制作動態(tài)滑竿條的相關代碼完成內(nèi)容,需要的朋友可以參考一下
    2021-12-12
  • Element 默認勾選表格 toggleRowSelection的實現(xiàn)

    Element 默認勾選表格 toggleRowSelection的實現(xiàn)

    這篇文章主要介紹了Element 默認勾選表格 toggleRowSelection的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • 如何修改vant的less樣式變量

    如何修改vant的less樣式變量

    這篇文章主要介紹了如何修改vant的less樣式變量方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • el-date-picker設置日期默認值兩種方法(當月月初至月末)

    el-date-picker設置日期默認值兩種方法(當月月初至月末)

    這篇文章主要給大家介紹了關于el-date-picker設置日期默認值(當月月初至月末)的相關資料,文中通過代碼示例將解決的辦法介紹的非常詳細,對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08

最新評論