欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue中PC端使用高德地圖實現(xiàn)搜索定位、地址標(biāo)記、彈窗顯示定位詳情(完整實例)

 更新時間:2023年07月07日 09:58:10   作者:不等天亮等時光  
這篇文章主要介紹了vue中PC端使用高德地圖實現(xiàn)搜索定位、地址標(biāo)記、彈窗顯示定位詳情,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

PC端高德地圖使用步驟:

1、注冊并登錄高德開放平臺獲取
2、安裝高德依賴(amap-jsapi-loader)
3、初始化地圖
4、首次打開地圖獲取當(dāng)前定位并標(biāo)記
5、根據(jù)已有地址自動定位到指定地址并標(biāo)記
6、新增、清除標(biāo)記及自定義信息窗體
7、鼠標(biāo)點(diǎn)擊地圖并選點(diǎn)標(biāo)記
8、根據(jù)關(guān)鍵字搜索并自動定位
9、效果圖
10、完整代碼

高德官方api高德官方開放平臺

一、注冊并登錄高德開放平臺獲?。╧ey和秘鑰)

在這里插入圖片描述

引用:

<script>
  import AMapLoader from '@amap/amap-jsapi-loader';
  window._AMapSecurityConfig = {
    securityJsCode: '**************************',//你的秘鑰
  }
 </script>

二、安裝高德依賴

npm i &#64;amap/amap-jsapi-loader --save

三、初始化地圖

封裝公共的地圖組件:

 <map-container :positionInfo ='positionInfo' @select ='getLocationInfo'></map-container>
 mounted() {
      this.initMap()
 },
 methods:{
    initMap(){
      AMapLoader.load({
        key:"**********************", // 申請好的Web端開發(fā)者Key,首次調(diào)用 load 時必填
        version:"2.0",      // 指定要加載的 JSAPI 的版本,缺省時默認(rèn)為 1.4.15
        plugins: [          //需要使用的插件
          'AMap.ToolBar',
          'AMap.Scale',
          'AMap.Geolocation',
          'AMap.PlaceSearch',
          'AMap.AutoComplete',
          'AMap.Geocoder',
          'AMap.CitySearch'
        ],
        resizeEnable: true,
      }).then((AMap)=>{
        const that = this;
        that.map = new AMap.Map("container",{  //設(shè)置地圖容器id
          viewMode:"3D",    //是否為3D地圖模式
          zoom:12,          //初始化地圖級別
        });
     }).catch(e=>{
      console.log(e);
     })
 },

四、首次打開地圖獲取當(dāng)前定位并標(biāo)記

initMap(){
   AMapLoader.load({
      key:"*******************", // 申請好的Web端開發(fā)者Key,首次調(diào)用 load 時必填
      version:"2.0",      // 指定要加載的 JSAPI 的版本,缺省時默認(rèn)為 1.4.15
      plugins: [
        'AMap.ToolBar',
        'AMap.Scale',
        'AMap.Geolocation',
        'AMap.PlaceSearch',
        'AMap.AutoComplete',
        'AMap.Geocoder',
        'AMap.CitySearch'
      ],
      resizeEnable: true,
    }).then((AMap)=>{
      const that = this;
      that.map = new AMap.Map("container",{  //設(shè)置地圖容器id
        viewMode:"3D",    //是否為3D地圖模式
        zoom:12,          //初始化地圖級別
      });
      that.getCurrentLocation();//獲取當(dāng)前定位
      that.geocoder = new AMap.Geocoder()
      that.map.addControl(new AMap.Scale())  // 在圖面添加比例尺控件,展示地圖在當(dāng)前層級和緯度下的比例尺
      that.map.addControl(new AMap.ToolBar())  //在圖面添加鷹眼控件,在地圖右下角顯示地圖的縮略圖
    }).catch(e=>{
      console.log(e);
    })
  },
  //獲取當(dāng)前定位
  getCurrentLocation(){
    const that = this;
    that.geolocation = new AMap.Geolocation({
      timeout: 3000,          //超過3秒后停止定位,默認(rèn):5s
      enableHighAccuracy: true,
      zoomToAccuracy: true,   //定位成功后是否自動調(diào)整地圖視野到定位點(diǎn)
    });
    that.geolocation.getCurrentPosition(function(status,result){
    //備注:getCurrentPosition方法會調(diào)用超時或失?。?
    //Get geolocation time out:瀏覽器定位超時,包括原生的超時,可以適當(dāng)增加超時屬性的設(shè)定值以減少這一現(xiàn)象。
    //另外還有個別瀏覽器(如google Chrome瀏覽器等)本身的定位接口是黑洞,通過其請求定位完全沒有回應(yīng),也會超時返回失敗。
    //Get geolocation failed:定位失敗,Chrome、火狐以及部分套殼瀏覽器接入的定位服務(wù)在國外,有較大限制,失敗率高。
      console.log(status,result);
      if(status=='complete'){
        that.onComplete(result)
      }else{
        that.onError(result) //失敗后可使用getCityInfo獲取非精準(zhǔn)定位(具體到省市)
      }
    });
  },
  //解析定位結(jié)果
  onComplete(data) {
    console.log('定位結(jié)果:' + data.position) //經(jīng)緯度信息
    let lnglat = data.position;
    let marker = new AMap.Marker({  //創(chuàng)建標(biāo)記
      position: new AMap.LngLat(lnglat[0], lnglat[1])
    })
    this.map.clearMap()// 清除所有覆蓋物(點(diǎn)標(biāo)志)
    this.map.add(marker)// 添加點(diǎn)標(biāo)志
    let that = this
    //經(jīng)緯度轉(zhuǎn)換為中文地址詳情
    that.geocoder.getAddress(lnglat, function (status, result) {
      if (status === 'complete' && result.regeocode) {
        that.address = result.regeocode.formattedAddress;
        that.showInfoWindow(marker);//自定義信息窗體
      } else {
        that.$message.error('根據(jù)經(jīng)緯度查詢地址失敗')
      }
    })
  },
  //解析定位錯誤信息
  onError(data) {
    this.getLngLatLocation()
  },
  //在獲取具體定位失敗時調(diào)用的代碼:(非精準(zhǔn)定位!?。。?
  getLngLatLocation() {
    const that = this;
    that.geolocation.getCityInfo(function (status, result) {
      if (status === 'complete') {
        let data = result.position
        that.address = result.province + result.city;
        that.showLocation(data)
      } else {
        that.$message.error('獲取地址失敗')
      }
    })
  },

五、根據(jù)已有地址自動定位到指定地址并標(biāo)記

initMap(){
   AMapLoader.load({
      key:"*******************", // 申請好的Web端開發(fā)者Key,首次調(diào)用 load 時必填
      version:"2.0",      // 指定要加載的 JSAPI 的版本,缺省時默認(rèn)為 1.4.15
      plugins: [
        'AMap.ToolBar',
        'AMap.Scale',
        'AMap.Geolocation',
        'AMap.PlaceSearch',
        'AMap.AutoComplete',
        'AMap.Geocoder',
        'AMap.CitySearch'
      ],
      resizeEnable: true,
    }).then((AMap)=>{
      const that = this;
      that.map = new AMap.Map("container",{  //設(shè)置地圖容器id
        viewMode:"3D",    //是否為3D地圖模式
        zoom:15,           //初始化地圖級別
        center:[that.positionInfo.lng,that.positionInfo.lat];   // 首次加載地圖自動加載到指定位置中心
      });
      that.showLocation(data) //新增標(biāo)記并展示信息窗體
      that.map.addControl(new AMap.Scale())  // 在圖面添加比例尺控件,展示地圖在當(dāng)前層級和緯度下的比例尺
      that.map.addControl(new AMap.ToolBar())  //在圖面添加鷹眼控件,在地圖右下角顯示地圖的縮略圖
    }).catch(e=>{
      console.log(e);
    })
  },

六、新增、清除標(biāo)記及自定義信息窗體

//新增標(biāo)記
showLocation(data){
   let marker = new AMap.Marker({
     position: new AMap.LngLat( data[0],data[1]) //參數(shù)為經(jīng)緯度
   })
   this.map.clearMap()// 清除所有覆蓋物(點(diǎn)標(biāo)志)
   this.map.add(marker)// 添加點(diǎn)標(biāo)志
   this.showInfoWindow(marker);//自定義信息窗體
 },
//自定義信息窗體
showInfoWindow(marker){
  let infoWindow = new AMap.InfoWindow({
    isCustom: true, //是否自定義信息窗體
    content:  `<div style="background-color: white;padding: 0 5px; border-radius: 5px;border: 1px solid #cccccc;"> 地址:${this.address}</div>`,
    closeWhenClickMap: true,
    zIndex: 999,
    offset: new AMap.Pixel(16, -35)
  });
  infoWindow.open(this.map, marker.getPosition());
},

七、鼠標(biāo)點(diǎn)擊地圖并選點(diǎn)標(biāo)記

 initMap(){
   AMapLoader.load({
      key:"*******************", // 申請好的Web端開發(fā)者Key,首次調(diào)用 load 時必填
      version:"2.0",      // 指定要加載的 JSAPI 的版本,缺省時默認(rèn)為 1.4.15
      plugins: [
        'AMap.ToolBar',
        'AMap.Scale',
        'AMap.Geolocation',
        'AMap.PlaceSearch',
        'AMap.AutoComplete',
        'AMap.Geocoder',
        'AMap.CitySearch'
      ],
      resizeEnable: true,
    }).then((AMap)=>{
      const that = this;
      that.map = new AMap.Map("container",{  //設(shè)置地圖容器id
        viewMode:"3D",    //是否為3D地圖模式
        zoom:12,           //初始化地圖級別
      });
      that.geocoder = new AMap.Geocoder()
      that.map.addControl(new AMap.Scale())  // 在圖面添加比例尺控件,展示地圖在當(dāng)前層級和緯度下的比例尺
      that.map.addControl(new AMap.ToolBar())  //在圖面添加鷹眼控件,在地圖右下角顯示地圖的縮略圖
       that.handleClick(AMap)//地圖選點(diǎn)
    }).catch(e=>{
      console.log(e);
    })
  },
   //點(diǎn)擊地圖獲取地理位置
  handleClick(){
    this.map.on('click', (e) => {
      let lng = e.lnglat.lng
      let lat = e.lnglat.lat
      let marker = new AMap.Marker({
        position: new AMap.LngLat(lng, lat)
      })
      this.map.clearMap()// 清除所有覆蓋物(點(diǎn)標(biāo)志)
      this.map.add(marker)// 添加點(diǎn)標(biāo)志
      let lnglat = [lng, lat]
      let that = this
      that.geocoder.getAddress(lnglat, function (status, result) {
        if (status === 'complete' && result.regeocode) {
          that.address = result.regeocode.formattedAddress;
          that.showInfoWindow(marker);//自定義信息窗體
         let thisPosition = {
           address: that.address,
           lng: lng,
           lat: lat
          };
          that.$emit("select",thisPosition) //返回給父組件
        } else {
          that.$message.error('根據(jù)經(jīng)緯度查詢地址失敗')
        }
      })
    })
  },

八、根據(jù)關(guān)鍵字搜索并自動定位

//搜索框及搜索結(jié)果選擇窗
<template>
  <div>
  	//搜索框
    <div class="map-box">
      <div class="label">關(guān)鍵字搜索</div>
      <el-input
        v-model="mapAddress"
        placeholder="請輸入內(nèi)容"
        id="tipinput"
        @keyup.enter.native="searchKeyWord"
      >
      </el-input>
      <el-button type="primary" @click="searchKeyWord"  icon="el-icon-search" ></el-button>
    </div>
    //搜索結(jié)果選擇窗
    <div class="map_search_result"  v-if="showSearchResult">
      <ul>
        <li  @click="markerResult(item)" v-for="(item,index) in poiList" :key="index">{{item.name}}</li>
      </ul>
    </div>
    //地圖
    <div id="container" :style="{width: width, height: height}"></div>
  </div>
</template>
initMap(){
   AMapLoader.load({
      key:"*******************", // 申請好的Web端開發(fā)者Key,首次調(diào)用 load 時必填
      version:"2.0",      // 指定要加載的 JSAPI 的版本,缺省時默認(rèn)為 1.4.15
      plugins: [
        'AMap.ToolBar',
        'AMap.Scale',
        'AMap.Geolocation',
        'AMap.PlaceSearch',
        'AMap.AutoComplete',
        'AMap.Geocoder',
        'AMap.CitySearch'
      ],
      resizeEnable: true,
    }).then((AMap)=>{
      const that = this;
      that.map = new AMap.Map("container",{  //設(shè)置地圖容器id
        viewMode:"3D",    //是否為3D地圖模式
        zoom:12,           //初始化地圖級別
      });
      that.handleClick(AMap)//地圖選點(diǎn)
      that.map.addControl(new AMap.Scale())  // 在圖面添加比例尺控件,展示地圖在當(dāng)前層級和緯度下的比例尺
      that.map.addControl(new AMap.ToolBar())  //在圖面添加鷹眼控件,在地圖右下角顯示地圖的縮略圖
      that.geocoder = new AMap.Geocoder()
      that.mapSearchInit()
    }).catch(e=>{
      console.log(e);
    })
  },
 /** 初始化搜索 */
  mapSearchInit(){
     let autoOptions = {
       input: "tipInput",
     }
     let autoCompleteComponent= new AMap.Autocomplete(autoOptions);
     this.autoCompleteComponent = autoCompleteComponent;
     // 注冊placeSearch組件
     this.placeSearchComponent = new AMap.PlaceSearch()
   },
   //根據(jù)輸入內(nèi)容查詢
   searchKeyWord(){
     let that= this
     that.placeSearchComponent.search(that.mapAddress, function (status, result) {
       if(status==='complete' && result.info === "OK"){
         that.showsearchResult = true
         that.poiList = result.poiList.pois
       }else{
         that.showsearchResult = false
         that.poiList = []
         that.$message({
           message: "沒有查到結(jié)果",
           type: "warning",
         });
       }
     })
   },
   //選擇搜索的內(nèi)容
   markerResult(data){
     this.showsearchResult = false;
     this.address = data.name;
     var marker = new AMap.Marker({
       position: [Number(data.location.lng),Number(data.location.lat)],
     });
     this.map.clearMap()// 清除所有覆蓋物(點(diǎn)標(biāo)志)
     this.map.add(marker)// 添加點(diǎn)標(biāo)志
     this.showInfoWindow(marker);
     setTimeout(() => {
       this.map.setCenter(data.location);
       this.map.setZoom(15);
     }, 50)
     let thisPosition = {
       address: this.address,
       lng: data.location.lng,
       lat: data.location.lat
     };
     this.$emit("select",thisPosition)
   },

九、效果圖

在這里插入圖片描述

到此這篇關(guān)于vue中PC端使用高德地圖 -- 實現(xiàn)搜索定位、地址標(biāo)記、彈窗顯示定位詳情的文章就介紹到這了,更多相關(guān)vue高德地圖搜索定位內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue實現(xiàn)讓頁面加載時請求后臺接口數(shù)據(jù)

    Vue實現(xiàn)讓頁面加載時請求后臺接口數(shù)據(jù)

    這篇文章主要介紹了Vue實現(xiàn)讓頁面加載時請求后臺接口數(shù)據(jù)
    2022-08-08
  • 一文詳解Vue3中的14種組件通信方式

    一文詳解Vue3中的14種組件通信方式

    對于日常使用vue3開發(fā)項目的前端小伙伴來說,組件通信方式可以說是必會的基本功,今天帶大家一起盤下vue3中的14種通信方式,希望對大家有所幫助
    2025-01-01
  • Bootstrap+Vue滑動監(jiān)聽Scrollspy實現(xiàn)餐廳餐品展示

    Bootstrap+Vue滑動監(jiān)聽Scrollspy實現(xiàn)餐廳餐品展示

    本文主要介紹了Bootstrap+Vue滑動監(jiān)聽Scrollspy實現(xiàn)餐廳餐品展示,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • element中的el-upload附件上傳與附件回顯

    element中的el-upload附件上傳與附件回顯

    這篇文章主要介紹了element中的el-upload附件上傳與附件回顯方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • element-ui樹形控件后臺返回的數(shù)據(jù)+生成組織樹的工具類

    element-ui樹形控件后臺返回的數(shù)據(jù)+生成組織樹的工具類

    這篇文章主要介紹了element-ui樹形控件后臺返回的數(shù)據(jù)+生成組織樹的工具類,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Vue中$router和$route的區(qū)別詳解

    Vue中$router和$route的區(qū)別詳解

    在 Vue.js 中,$router 和 $route 是兩個常用的對象,用于處理路由相關(guān)的操作,下面小編就來和大家介紹一下$router 和 $route 的區(qū)別以及如何使用它們吧
    2023-06-06
  • Vue如何設(shè)置滾動條自動保持到最底端

    Vue如何設(shè)置滾動條自動保持到最底端

    在開發(fā)中我們常常會遇到需要讓滾動條保持到最底端的需求,比如在開發(fā)一個聊天框時,請求接口拿到消息列表數(shù)據(jù),展示到前端頁面時,需要讓滾動條自動滾到最底端,以此來展示最后的聊天記錄,這篇文章主要介紹了Vue如何設(shè)置滾動條自動保持到最底端,需要的朋友可以參考下
    2024-08-08
  • el-date-picker日期時間選擇器的選擇時間限制到分鐘級別

    el-date-picker日期時間選擇器的選擇時間限制到分鐘級別

    文章介紹了如何使用el-date-picker 組件來限制用戶選擇的時間,禁止選擇當(dāng)前時間的日期及時分,同時允許選擇其他日期的全天時分,通過設(shè)置 `pickerOptions` 對象的屬性,可以實現(xiàn)對日期和時間的精確控制,感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • Vite配置路徑別名的簡單實現(xiàn)方法

    Vite配置路徑別名的簡單實現(xiàn)方法

    Vite項目中我們可以手動將src路徑設(shè)置**@**路徑別名,可以省下很多引入路徑的冗余路徑,下面這篇文章主要給大家介紹了關(guān)于Vite配置路徑別名的簡單實現(xiàn)方法,需要的朋友可以參考下
    2023-04-04
  • vue19 組建 Vue.extend component、組件模版、動態(tài)組件 的實例代碼

    vue19 組建 Vue.extend component、組件模版、動態(tài)組件 的實例代碼

    這篇文章主要介紹了vue19 組建 Vue.extend component、組件模版、動態(tài)組件 的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04

最新評論