vue中使用vue amap组件调用高德地图

vue中使用vue amap组件调用高德地图:
通过标签的方式
附上文档:https://elemefe.github.io/vue-amap/#/zh-cn/introduction/install
(1)、地图安装及显示
1、npm install vue-amap --save
2、在main.js中初始化高德地图

import AMap from 'vue-amap';
Vue.use(AMap);
// 初始化vue-amap
AMap.initAMapApiLoader({
    // 申请高德的key
    key: 'key',
    // 插件集合 (插件按需引入)
    plugin: ['AMap.Autocomplete','AMap.Geolocation','Geocoder', 'AMap.PlaceSearch', 'AMap.Marker', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor']
  });

3、下面我们把地图先渲染到DOM上
zoom为地图的缩放等级取值范围3-18移动端取值3-19
plugin为地图的基本配置
center是地图的中心点[经度,纬度]
events是地图的事件

<!--地图显示-->
<el-amap vid="amapDemo" :zoom="zoom" :plugin="plugin"
 :center="center" :clickable="enableclick" 
 :events="markerEvents">
</el-amap>

(1)、地图的基本配置

zoom: 4,
center: [116.397428, 39.909232],
plugin: [
        {
          enableHighAccuracy: true, //是否使用高精度定位,默认:true
          timeout: 100, //超过10秒后停止定位,默认:无穷大
          maximumAge: 0, //定位结果缓存0毫秒,默认:0
          convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
          showButton: true, //显示定位按钮,默认:true
          buttonPosition: "RB", //定位按钮停靠位置,默认:'LB',左下角
          showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
          showCircle: true, //定位成功后用圆圈表示定位精度范围,默认:true
          panToLocation: true, //定位成功后将定位到的位置作为地图中心点,默认:true
          zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:f
          extensions: "all",
          pName: "Geolocation",
          events: {
            init(o) {
              // o 是高德地图定位插件实例
              o.getCurrentPosition((status, result) => {
                if (result && result.position) {
                  self.lng = result.position.lng;
                  self.lat = result.position.lat;
                  console.log("本地坐标为:" + [self.lng, self.lat]);
                  self.center = [self.lng, self.lat];
                  self.loaded = true;
                  self.address = result.formattedAddress;
                  //此处为本地坐标显示在坐标上方的文字
                  html代码如下↓
                   <el-amap-text :text="texts.text" :offset="texts.offset" :position="texts.position">
                   </el-amap-text>
                   offset为偏移量;取值方式为数组形式:[5-30]
                   js代码↓
                  self.texts.position = [self.lng, self.lat];
                  self.texts.text = "我在 " + result.formattedAddress + " 附近";
                  // console.log(self.address)
                  self.$nextTick();
                }
              });
            }
          }
        }
      ],

(2)地图绑定的点击事件
主要通过高德SDK内置的方法获取到坐标点和地址

 markerEvents: {
    click(e) {
      self.markers = [];
      console.log([e.lnglat.lng, e.lnglat.lat])//可以拿到点击的坐标
      let marker = {
        position: [e.lnglat.lng, e.lnglat.lat]
      };
      self.markers.push(marker);
      //     这里通过高德 SDK 完成。通过高德SDK内置的方法获取到坐标点和地址
      var geocoder = new AMap.Geocoder({
        radius: 1000,
        extensions: "all"
      });
      geocoder.getAddress([e.lnglat.lng, e.lnglat.lat], function(
        status,
        result
      ) {
        if (status === "complete" && result.info === "OK") {
          if (result && result.regeocode) {
              //这里我给弹框赋值点击的坐标和地址
            self.window.position = [e.lnglat.lng, e.lnglat.lat];
            self.window.content = result.regeocode.formattedAddress
            self.$nextTick();
          }
        }
      });
    }
  },

(3)在地图上点击添加点位

let marker = {
position: [e.lnglat.lng, e.lnglat.lat]
};
self.markers.push(marker);

以上代码中点击时往markers里添加了点坐标那么我们只需要循环拿出来即可

<el-amap-marker v-for="(marker,index) in markers" :position="marker.position" :key="index">
</el-amap-marker>

(4)下面是搜索显示点位的功能:
searchOption搜索条件
onSearchResult搜索回调函数


<el-amap-search-box class="search-box" 
:search-option="searchOption" 
:on-search-result="onSearchResult">
</el-amap-search-box>
属性:
     searchOption: {
        city: "海淀区",//限制城市
        citylimit: true//是否限制城市内搜索
      },
方法:
    onSearchResult(pois) {
      console.log(this.searchOption.ctiy);
      this.markers.length = [];
      this.zoom = 17;
      this.center = [pois[0].lng, pois[0].lat];
      let latSum = 0;
      let lngSum = 0;
      if (pois.length > 0) {
        pois.forEach(poi => {
          let { lng, lat } = poi;
          lngSum += lng;
          latSum += lat;
          let name = poi;
          let marker = {
            position: [poi.lng, poi.lat],
            name: poi.name
          };
          this.markers.push(marker);
        });
        let center = {
          lng: lngSum / pois.length,
          lat: latSum / pois.length
        };
        this.mapCenter = [center.lng, center.lat];
      }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Vue项目使用vue-amap,首先需要安装vue-amap模块。可以通过npm命令来安装: ``` npm install vue-amap --save ``` 安装完成后,在Vue项目的入口文件引入vue-amap模块,并进行初始化: ``` import VueAMap from 'vue-amap'; Vue.use(VueAMap); VueAMap.initAMapApiLoader({ // 高德的key key: 'your amap key', // 插件集合 plugin: ['AMap.Geolocation', 'AMap.MarkerClusterer'], // 高德 sdk 版本,默认为 1.4.15 v: '1.4.15', }); ``` 其,`your amap key`需要替换成你自己的高德地图应用的key。 接下来,在组件引入`vue-amap`的组件,并使用`amap`标签来显示地图: ``` <template> <div> <amap :zoom="zoom" :center="center"> <amap-marker :position="center"></amap-marker> </amap> </div> </template> <script> export default { data() { return { zoom: 13, center: [116.397428, 39.90923], }; }, }; </script> ``` 以上代码实现了在Vue组件显示一个地图,并在地图上显示一个标记。具体使用方式可以参考vue-amap的文档。 ### 回答2: Vue-amap 是一个基于Vue高德地图组件库,可以方便地在Vue项目使用高德地图服务。 要使用vue-amap,需要按照以下步骤进行操作: 1. 首先,安装vue-amap库。可以通过npm或者yarn来进行安装。在终端执行以下命令: ``` npm install --save vue-amap ``` 或者 ``` yarn add vue-amap ``` 2. 在Vue项目的入口文件(通常是main.js),引入vue-amap并注册为Vue的插件。在代码添加以下内容: ```javascript import VueAMap from 'vue-amap'; Vue.use(VueAMap); ``` 3. 在需要使用高德地图组件,可以通过`VueAMap.initAMapApiLoader`方法进行配置和初始化。在代码添加以下内容: ```javascript VueAMap.initAMapApiLoader({ key: 'your-amap-key', // 高德地图API的key // 插件集合 plugin: ['AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.Geolocation'], // 高德地图 SDK 版本,默认为 1.4.4 v: '1.4.4' }); ``` 在这里,需要将`your-amap-key`替换为你申请到的高德地图API的key。 4. 然后,就可以在组件使用vue-amap提供的地图组件了。例如,在组件的模板添加以下内容: ```html <vue-amap id="mapContainer" :zoom="13" :center="['116.397428', '39.90923']"> <div class="amap-marker" :position="['116.397428', '39.90923']"></div> </vue-amap> ``` 在这里,使用`vue-amap`标签来包裹地图组件,并通过`:zoom`和`:center`属性设置地图缩放级别和心点。在`vue-amap`标签内,可以添加其他地图相关的组件。 这样,就可以在Vue项目使用vue-amap来显示高德地图了。在实际开发,还可以根据需要使用vue-amap提供的其他功能和API来实现更多的地图操作和交互。 ### 回答3: Vue-amap是一个基于Vue.js的高德地图组件库,提供了丰富的地图展示和操作功能。要使用vue-amap,首先需要在项目安装它。 1. 在项目目录下打开终端,运行以下命令来安装vue-amap: ``` npm install vue-amap --save ``` 2. 在需要使用地图的组件,引入vue-amap: ```javascript import AMap from 'vue-amap'; ``` 3. 调用Vue.use()方法并添加参数AMap: ```javascript Vue.use(AMap); ``` 4. 在项目的入口文件(main.js),加载高德地图的SDK: ```javascript AMap.initAMapApiLoader({ key: 'your-amap-api-key', plugin: ['AMap.Geolocation'] }); ``` 注意:replace 'your-amap-api-key'为你注册高德地图开发者账号后获得的API Key。 5. 在组件的template使用vue-amap提供的地图组件: ```html <template> <div> <amap-map :zoom="13" :center="[lng, lat]"> <amap-marker :position="[lng, lat]"></amap-marker> </amap-map> </div> </template> ``` 上述代码展示了一个简单的地图,包含一个标记点在指定的经纬度上。 至此,我们已经完成了vue-amap的基本配置,可以在Vue项目使用高德地图的各种功能了。根据官方文档,还可以使用更多的组件和API来控制地图的交互、显示等,以满足具体需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值