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

vue使用高德地圖實(shí)現(xiàn)添加點(diǎn)標(biāo)記和獲取點(diǎn)擊位置信息的示例代碼

 更新時(shí)間:2024年01月23日 15:07:12   作者:浮橋  
這篇文章主要介紹了vue使用高德地圖實(shí)現(xiàn)添加點(diǎn)標(biāo)記和獲取點(diǎn)擊位置信息的示例代碼,文中補(bǔ)充介紹了高德vue-amap使用(一)標(biāo)記點(diǎn)位獲取地址及經(jīng)緯度,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧

vue項(xiàng)目中使用高德地圖實(shí)現(xiàn)添加點(diǎn)標(biāo)記和獲取點(diǎn)擊位置信息

<template>
    <div class="container">
        <div id="map-container">地圖</div>
    </div>
</template>
<script>
export default {
  name: '',
  components: {},
  data () {
    return {
      map: null,
      markers: [],
      markersPosition: [],
      geoCoder: null
    }
  },
  mounted () {
    this.mapInit()
  },
  methods: {
    mapInit () {
      // eslint-disable-next-line no-undef
      this.map = new AMap.Map('map-container', {
        zoom: 11, // 級(jí)別
        // center: [116.397428, 39.90923], // 中心點(diǎn)坐標(biāo)
        // layers: [ // 使用多個(gè)圖層
        //   // 衛(wèi)星
        //   new AMap.TileLayer.Satellite(),
        //   // 路網(wǎng)
        //   new AMap.TileLayer.RoadNet()
        // ],
        resizeEnable: true
        // viewMode: '3D'// 使用3D視圖
      })
      // eslint-disable-next-line no-undef
      this.geoCoder = new AMap.Geocoder()
      this.handlerMapClick()
    },
    handlerMapClick () {
      this.map.on('click', (e) => {
        // 點(diǎn)擊坐標(biāo)
        this.markersPosition = [e.lnglat.lng, e.lnglat.lat]
        this.removeMarker()
        // 設(shè)置新的標(biāo)記
        this.setMapMarker()
        // eslint-disable-next-line no-undef
        // 根據(jù)坐標(biāo)獲取位置信息
        this.geoCoder.getAddress(this.markersPosition, (status, result) => {
          if (status === 'complete' && result.regeocode) {
            this.address = result.regeocode.formattedAddress
            console.log('點(diǎn)擊位置信息:', result.regeocode.formattedAddress)
            // id
            let adcode = result.regeocode.addressComponent.adcode
            //   let reg = /.+?(省|市|自治區(qū)|自治州|縣|區(qū))/g
            let provinceId = parseInt(adcode.substr(0, 2) + '0000')
            let cityId = parseInt(adcode.substr(0, 4) + '00')
            let areaId = adcode
            console.log('點(diǎn)擊位置的省市區(qū)id:', provinceId, cityId, areaId)
          }
        })
      })
    },
    // 設(shè)置點(diǎn)擊位置的標(biāo)記
    setMapMarker () {
      let marker = new AMap.Marker({
        map: this.map,
        position: this.markersPosition
      })
      // 將 markers 添加到地圖
      this.markers.push(marker)
    },
    // 添加標(biāo)記
    addMarker () {
      //   經(jīng)度 緯度
      let lng = Math.random() * 135.05 + 73.3
      let lat = Math.random() * 53.33 + 3.51
      console.log('添加標(biāo)記', [lng, lat])
      // 添加標(biāo)記
      this.map.setFitView()
      let marker = new AMap.Marker({
        map: this.map,
        position: [lng, lat]
        // content: markerContent
        // eslint-disable-next-line
                // offset: new AMap.Pixel(-13, -30)
      })
      // 將 markers 添加到地圖
      this.markers.push(marker)
      this.map.setFitView()`在這里插入代碼片`
      // 鼠標(biāo)點(diǎn)擊marker彈出自定義的信息窗體
      // eslint-disable-next-line no-undef
      AMap.event.addListener(marker, 'click', function (e) {
        console.log('點(diǎn)擊marker', e)
      })
    },
    // 刪除之前后的標(biāo)記點(diǎn)
    removeMarker () {
      if (this.markers) {
        this.map.remove(this.markers)
      }
    }
  }
}
</script>
<style scoped lang="scss">
#map-container {
    width: 70vw;
    height: 70vh;
}
</style>

高德vue-amap使用(一)標(biāo)記點(diǎn)位獲取地址及經(jīng)緯度

圖片示例

準(zhǔn)備工作

高德開(kāi)放平臺(tái):https://lbs.amap.com/

注冊(cè)登錄后進(jìn)入控制臺(tái),在應(yīng)用管理下我的應(yīng)用里創(chuàng)建應(yīng)用添加key,就可以看到你的安全密鑰了

安裝與配置

npm安裝

npm install vue-amap --save

main.js配置

import VueAMap from 'vue-amap'; //引入高德地圖
Vue.use(VueAMap);
VueAMap.initAMapApiLoader({
  key: '14efe7b9c0a51017c1ee7c641758ba69',
  plugin: [ // 這里根據(jù)自己項(xiàng)目按需引入插件
    'AMap.Autocomplete', 
    'AMap.PlaceSearch', 
    'AMap.Scale',
    'AMap.OverView', 
    'AMap.ToolBar',
    'AMap.MapType',
    'AMap.PolyEditor', 
    'AMap.CircleEditor',// 圓形編輯器插件
	"AMap.Geolocation", // 定位控件,用來(lái)獲取和展示用戶主機(jī)所在的經(jīng)緯度位置
	"AMap.Geocoder", // 地理編碼與逆地理編碼服務(wù),用于地址描述與坐標(biāo)間的相互轉(zhuǎn)換
	"AMap.CitySearch",
  ]
});
window._AMapSecurityConfig = {
  securityJsCode:'你的安全密鑰',
}
// 解決高德地圖刷新顯示不出來(lái)
const amapKeys = Object.keys(localStorage).filter(key => key.match(/^_AMap_/))
amapKeys.forEach(key => { localStorage.removeItem(key) })

使用

父組件

1.html部分

<el-form-item label="設(shè)備位置">
  <el-button class="dev-product" @click="openMap()">
    <span class="text"  :class="mapInfo.lat?'active':''">{{addressTip}}</span><i class="el-icon-more"></i>
  </el-button>
  <div v-if="mapInfo.lat&&mapInfo.lng">經(jīng)度:{{mapInfo.lng}},緯度:{{mapInfo.lat}}</div>
</el-form-item>
<el-dialog title="設(shè)備位置" :visible.sync="map" width="900px" id="map">
   <amap @mapDing="getCoordinate" :Nowlnglat="Nowlnglat"></amap>
   <div slot="footer" class="dialog-footer">
     <el-button @click="map = false" class="button-minwidth">{{$t('Base.cancels')}}</el-button>
     <el-button type="primary" @click="submitMap" class="button-minwidth">{{$t('Base.confirms')}}</el-button>
   </div>
</el-dialog>

2.js部分

<script>
import amap from "../map.vue"
export default {
  components: {amap},
  data() {
    return {
      addressTip:'請(qǐng)選擇設(shè)備位置',
      map:false,
      mapInfo:{},
      Nowlnglat:[],//經(jīng)緯度傳值
      lng:113.03,
      lat:28.13
    }
  },
  methods: {
    openMap(){
      this.map=true
      if(this.lng&&this.lat){
        this.mapInfo={
          lng:this.lng,
          lat:this.lat
        }
        this.Nowlnglat=[this.lng,this.lat]
      }
    },
    getCoordinate(map){
      this.mapInfo=map
    },
    submitMap(){
      this.map=false
      this.addressTip=this.mapInfo.address
    }
  }
</script>

3.css部分

<style lang="scss" scoped>
  .dev-product{
    width: 300px;
    height: 32px;
    padding: 0 8px;
    display: flex;
    align-items: center;
  ::v-deep span{
    width: 100%;
    overflow: hidden;
    display: flex;
    justify-content: space-between;
    padding-left: 5px;
    .text{
      width: 92%;
      color: #c0c4cc;
    }.active{
      width: 92%;
      color: #303133 ;
    }
    i{
      color:#D8D8D8
    }
  }
}
</style>              

子組件

1.html部分

 <template>
  <div class="map-container">
    <div class="amap-page-container">
      <div class="input-search">
        <el-input class="inpu" placeholder="請(qǐng)輸入檢索地址" v-model="address">
        <template #suffix>
          <el-button icon="el-icon-search"  @click="searchMap()"></el-button>
        </template>
        </el-input>
      </div>
      <el-amap vid="amap" :plugin="plugin" class="amap-demo" :center="center" :zoom="zoom" :events='events'>
        <!-- 點(diǎn)擊顯示標(biāo)記 -->
        <el-amap-marker v-for="(marker, index) in markers" :key="marker.index" :position="marker.position"
          :icon="marker.icon" :title="marker.title" :events="marker.events" :visible="marker.visible"
          :draggable="marker.draggable" :vid="index"></el-amap-marker>
      </el-amap>
      <div class="map-address">詳細(xì)地址:{{address}}</div>
      <div class="map-mark">
        <div class="mark-item">
          <span>經(jīng)度</span>
          <el-input placeholder="請(qǐng)輸入經(jīng)度" v-model="lng" maxlength="20"></el-input>
        </div>
        <div class="mark-item">
          <span>緯度</span>
          <el-input placeholder="請(qǐng)輸入緯度" v-model="lat" maxlength="20"></el-input>
        </div> 
        <el-button class="mark" @click="handelQuery">查詢</el-button>
      </div>
    </div>
  </div>
</template>

2.js部分

<script>
  export default {
    name: "v-map",
    props: ["Nowlnglat"],
    data() {
      let self = this;
      return {
        map:{
          address:'',
          lng:'',
          lat:''
        },
        tishi: '',
        //地圖相關(guān)
        address: '', //獲取的位置
        zoom: 13, // 地圖縮放
        center: [122.59996, 26.197646], // 初始中心
        lng: 0, //經(jīng)緯度
        lat: 0,
        loaded: false,
        markers: [],// 點(diǎn)擊顯示的標(biāo)記默認(rèn)的定位
        //  自動(dòng)定位到當(dāng)前位置
        plugin: [
          {
          timeout: 1000, //超過(guò)10秒后停止定位
          panToLocation: true, //定位成功后將定位到的位置作為地圖中心點(diǎn)
          zoomToAccuracy: true, //定位成功后調(diào)整地圖視野范圍使定位位置及精度范圍視野內(nèi)可見(jiàn)
          pName: 'Geolocation',
          events: {
            init(o) {
              // o 是高德地圖定位插件實(shí)例
              o.getCurrentPosition((status, result) => {
                if (result && result.position) {
                  self.address = result.formattedAddress;
                  self.lng = result.position.lng;
                  self.lat = result.position.lat;
                  self.map.address= self.address
                  self.map.lng=self.lng
                  self.map.lat=self.lat
                  if(self.Nowlnglat[0] != null && self.Nowlnglat[1] != null){
                    let  Clnglat =self.Nowlnglat
                    self.center =  Clnglat 
                    self.markers = [{position: Clnglat,}]
                    let geocoder = new AMap.Geocoder({radius: 1000});
                    //根據(jù)坐標(biāo)獲取位置  將經(jīng)緯度 轉(zhuǎn)換后成 地址信息 放在 輸入框展示
                    geocoder.getAddress(Clnglat,function (status, result) {
                      if (status === "complete" && result.info === "OK") {
                        self.address=result.regeocode.formattedAddress
                        self.lng=self.Nowlnglat[0]
                        self.lat=self.Nowlnglat[1]
                        self.map.address= self.address
                        self.map.lng=self.lng
                        self.map.lat=self.lat
                      }
                    });
                  }else{
                    self.center = [self.lng, self.lat];
                    self.markers = [{position: self.center}]
                  }
                  self.loaded = true;
                  self.$nextTick();
                } else {
                  o.getCityInfo((status, result) => {
                    if (result && result.center) {
                      self.address = result.province+result.city;
                      self.lng = result.center[0];
                      self.lat = result.center[1];
                      self.map.address= self.address
                      self.map.lng=self.lng
                      self.map.lat=self.lat
                      if(self.Nowlnglat[0] != null && self.Nowlnglat[1] != null){
                        let  Clnglat =self.Nowlnglat
                        self.center =  Clnglat 
                        self.markers = [{position: Clnglat,}]
                        let geocoder = new AMap.Geocoder({radius: 1000});
                        //根據(jù)坐標(biāo)獲取位置  將經(jīng)緯度 轉(zhuǎn)換后成 地址信息 放在 輸入框展示
                        geocoder.getAddress(Clnglat,function (status, result) {
                          if (status === "complete" && result.info === "OK") {
                            self.address=result.regeocode.formattedAddress
                            self.lng=self.Nowlnglat[0]
                            self.lat=self.Nowlnglat[1]
                            self.map.address= self.address
                            self.map.lng=self.lng
                            self.map.lat=self.lat
                          }
                        });
                      }else{
                        self.center = result.center;
                        self.markers = [{position: self.center,}]
                      }
                      self.loaded = true;
                      self.$nextTick();
                    }
                  });
                }
              });
            }
          }
        }
        ],
        // 點(diǎn)擊地圖獲取當(dāng)前位置并顯示標(biāo)記
        events: {
          click(e) {
            self.chaadd(e.lnglat)
          }
        }
      }
    },
    created() {
      this.$emit('mapDing', this.map);
    },
    methods: {
      searchMap() {
        let that = this;
        let address = this.address;
        that.map.address=that.address
        var geocoder = new AMap.Geocoder({
          city: "", //城市設(shè)為北京,默認(rèn):“全國(guó)”
        });
        geocoder.getLocation(address, function(status, result) {
          if (status === 'complete' && result.geocodes.length) {
            var lnglat = result.geocodes[0].location;
            that.center = [lnglat.lng, lnglat.lat]
            that.lng = lnglat.lng;
            that.lat = lnglat.lat;
            that.markers = [{
              position: that.center,
            }]
            that.map.lng=that.lng
            that.map.lat=that.lat
          } else {
            that.address=undefined
            that.lng=undefined
            that.lat=undefined
            that.$message({
              type: "error",
              message: "根據(jù)地址查詢位置失敗",
            });
          }
        });
        that.$emit('mapDing', that.map);
      },
      chaadd(e) {
        let self = this;
        let { lng, lat} = e;
        self.lng = lng;
        self.lat = lat;
        self.map.lng=self.lng
        self.map.lat=self.lat
        self.center = [self.lng, self.lat];
        self.loaded = true;
        self.markers = [{position: self.center,}]
        var geocoder = new AMap.Geocoder({
          radius: 1000 //范圍,默認(rèn):500
        });
        var marker = new AMap.Marker();
        function regeoCode() {
          var lnglat = [lng, lat]
          geocoder.getAddress(lnglat, function(status, result) {
            if (status === 'complete' && result.regeocode) {
              self.address = result.regeocode.formattedAddress;
              self.map.address=self.address
            } else {
              self.address=undefined
              self.lng=undefined
              self.lat=undefined
              self.$message({
                type: "error",
                message: "根據(jù)經(jīng)緯度查詢地址失敗",
              });
            }
          });
        }
        regeoCode();
         self.$emit('mapDing', self.map);
      },
      handelQuery(){
        let self =this
        self.map.lng=parseFloat(self.lng)
        self.map.lat=parseFloat(self.lat)
        self.center = [parseFloat(self.lng), parseFloat(self.lat)];
        self.loaded = true;
        self.markers = [{
          position: self.center,
        }]
        var geocoder = new AMap.Geocoder({
          radius: 1000 //范圍,默認(rèn):500
        });
        // var marker = new AMap.Marker();
        function regeoCode() {
          var lnglat = [parseFloat(self.lng), parseFloat(self.lat)]
          geocoder.getAddress(lnglat, function(status, result) {
            if (status === 'complete' && result.regeocode) {
              self.address = result.regeocode.formattedAddress;
              self.map.address=self.address
            } else {
              self.address=undefined
              self.lng=undefined
              self.lat=undefined
              self.$message({
                type: "error",
                message: "根據(jù)經(jīng)緯度查詢地址失敗",
              });
            }
          });
        }
        regeoCode();
        self.$emit('mapDing', self.map);
      }
    }
  }
</script>

3.css部分

<style lang="scss" scoped>
.map-container{
  height: 526px;
  width: 100%;
  padding: 20px;
  display: flex;
  justify-content: center;
  .amap-page-container {
    height: 400px;
    width: 100%;
    position: relative;
    .input-search {
      position: absolute;
      top: 10px;
      right: 0px;
      z-index: 5;
      width: 400px;
      ::v-deep .el-input__inner{
        width: 271px !important;
        padding: 0 10px;
      }
      .inpu {
        width: calc(100% - 10px);
      }
      ::v-deep .el-input__suffix{
        position: absolute;
        height: 100%;
        right: 0 !important;
        top: 0;
      }
      .el-button--medium{
        height: 32px;
        width: 120px;
        background: #f2f6fc;
        display: flex;
        padding: 0;
        align-items: center;
        justify-content: center;
      }
    }
  }
  .map-address{
    margin-top: 15px;
    margin-bottom: 15px;
  }
  .map-mark{
    display: flex;
    flex-direction: row;
    .mark-item{
      width: 248px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-right: 20px;
      span{
        white-space: nowrap;
        margin-right: 20px;
      }
      ::v-deep .el-input__inner{
        width: 200px !important;
      }
      ::v-deep .el-input-number__decrease{
        display: none;
      }
      ::v-deep .el-input-number__increase{
        display: none;
      }
    }
    .mark{
      width: 80px;
      height: 32px;
      background: #087CF2;
      color: #ffffff;
      border: 1px solid #087cf2;
      padding: 0;
    }
  }
}
</style>                

到此這篇關(guān)于vue項(xiàng)目中使用高德地圖實(shí)現(xiàn)添加點(diǎn)標(biāo)記和獲取點(diǎn)擊位置信息的文章就介紹到這了,更多相關(guān)vue高德地圖獲取點(diǎn)擊位置信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論