【vue中高德地图的使用 el-amap】

高德地图vue使用 el-amap


前言

提示:需要注意使用当前高德版本 (2022-5-6 当前JS API最新版本为2.0)

高德JS API官网地址:https://lbs.amap.com/api/jsapi-v2/summary/

使用el-amap标签,进行高德地图的应用


一、在高德官网申请账号,key和样式地图

1、创建key和安全密钥
  • 官网地址: https://lbs.amap.com/
    创建key和秘钥
2、创建自定义地图
  • 创建地图,并却发布,在分享中复制JS API在项目中使用
    在这里插入图片描述
    在这里插入图片描述

二、先在项目中使用高德

1、项目中安装高德
npm install --save vue-amap
2、在main.js文件夹中引入注册,并引入所需API
import VueAMap from 'vue-amap'

Vue.use(VueAMap)
VueAMap.initAMapApiLoader({
  key: 'xxxxxx', //申请的key码需要填写的地方,格式为长串字符数字
  plugin: [//按照你的需要,引入地图的哪些功能,不需要下面这么多
    'AMap.Autocomplete', //输入提示插件
    'AMap.PlaceSearch', //POI搜索插件
    'AMap.Scale', //右下角缩略图插件 比例尺
    'AMap.OverView', //地图鹰眼插件
    'AMap.ToolBar', //地图工具条
    'AMap.MapType', //类别切换控件,实现默认图层与卫星图、实施交通图层之间切换的控制
    'AMap.PolyEditor', //编辑 折线多,边形
    'AMap.CircleEditor', //圆形编辑器插件
    'AMap.Geolocation', //定位控件,用来获取和展示用户主机所在的经纬度位置
    'MarkerClusterer',
    'AMap.HeatMap',
    'AMap.DistrictLayer',
    'AMap.Heatmap',
    'AMap.DistrictSearch',
    'AMap.Object3DLayer'
  ],
  v: '2.0', // 默认高德 sdk 版本为 1.4.4
  uiVersion: '1.0'
})
3、在index.html中引入

  <script type="text/javascript">
    window._AMapSecurityConfig = {
      securityJsCode: 'xxxxxxxxx', //所申请的安全密钥
    }
  </script>
  <script 
  	type="text/javascript"
    src="https://webapi.amap.com/maps?v=2.0&key=xxxxxxxxx&plugin=AMap.Geolocation,AMap.Autocomplete,AMap.PlaceSearch,AMap.Scale,AMap.OverView,AMap.ToolBar,AMap.MapType,AMap.PolyEditor,AMap.CircleEditor,AMap.MarkerClusterer,AMap.DistrictLayer,AMap.Heatmap,AMap.DistrictSearch,AMap.Object3DLayer"
  >//将key更换成自己申请的key
  </script>
  <script src="https://webapi.amap.com/ui/1.1/main.js"></script>
  <script src="//a.amap.com/jsapi_demos/static/resource/heatmapData.js"></script>
4、在所需要的页面使用
<template>
  <el-amap
    ref="map"
    vid="amapDemo"
    :amap-manager="amapManager"
    :events="mapEvents"
    :center="center"
    :mapStyle="mapStyle"
    expandZoomRange="true"
    :zoom="zoom"
    :plugin="plugins"
    :pitch="66"
  >
   <el-amap-marker :position="center" :icon="icon"> </el-amap-marker>
  </el-amap>
</template>
<script>
// import { AMapManager } from "vue-amap";
// let amapManager = new AMapManager();
export default {
	data() {
		return {
			center: [119.72899, 30.254775],
      		mapStyle: 'xxxxxxxx', //引入样式
			plugins: [
		        {
		          enableHighAccuracy: true, //是否使用高精度定位,默认:true
		          timeout: 100, //超过10秒后停止定位,默认:无穷大
		          maximumAge: 0, //定位结果缓存0毫秒,默认:0
		          convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
		          showButton: true, //显示定位按钮,默认:true
		          buttonPosition: "LB", //定位按钮停靠位置,默认:'LB',左下角
		          showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
		          showCircle: true, //定位成功后用圆圈表示定位精度范围,默认:true
		          panToLocation: false, //定位成功后将定位到的位置作为地图中心点,默认:true
		          zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:f
		          extensions: "all",
		          //地图定位按钮
		          pName: "Geolocation",
		          init(o) {
		            // o 是高德地图定位插件实例
		            o.getCurrentPosition((status, result) => {
		              console.log(result);
		              if (result && result.position) {
		                self.lng = result.position.lng;
		                self.lat = result.position.lat;
		                self.center = [self.lng, self.lat];
		                self.loaded = true;
		                self.$nextTick();
		              }
		            });
		          },
		        },
		        {
		          //地图工具条
		          pName: "ToolBar",
		          init(o) {},
		        },
		        {
		          //左下角缩略图插件 比例尺
		          pName: "Scale",
		          init(o) {},
		        },
	      ],
	  }
  },
}
</script>
  • 各种标签
点坐标 <!-- 点坐标 -->
<el-amap-marker v-for="(marker,index) in markers" :key="index" :position="marker.position"></el-amap-marker>

信息窗体 <!-- 信息窗体 -->
<el-amap-info-window v-for="( window,index) in windows" :key="index" :position="window.position" :content="window.content" :open="window.open"></el-amap-info-window>

折线<!-- 折线 -->
<el-amap-polyline :path="polyline.path"></el-amap-polyline>

多边形 <!-- 多边形 -->
<el-amap-polygon v-for="(polygon,index)  in polygons" :key="index" :path="polygon.path" :events="polygon.events"></el-amap-polygon><!-- 圆 -->
<el-amap-circle v-for="(circle,index) in circles" :key="index" :center="circle.center" :radius="circle.radius"></el-amap-circle>

图片覆盖物 <!-- 图片覆盖物 -->
<el-amap-ground-image v-for="(groundimage,index) in groundimages" :key="index" :url="groundimage.url" :bounds="groundimage.bounds"></el-amap-ground-image>

文本 <!-- 文本 -->
<el-amap-text v-for="(text,index) in texts" :key="index"></el-amap-text>

贝塞尔曲线 <!-- 贝塞尔曲线 -->
<el-amap-bezier-curve v-for="(line,index) in lines" :key="index"></el-amap-bezier-curve>

圆点标记 <!-- 圆点标记 -->
<el-amap-circle-marker v-for="(marker,index) in markers" :key="index"></el-amap-circle-marker>

椭圆 <!-- 椭圆 -->
<el-amap-ellipse v-for="(ellipse,index) in ellipses" :key="index"></el-amap-ellipse>

矩形 <!-- 矩形 -->
<el-amap-rectangle v-for="(rectangle,index) in rectangles" :key="index"></el-amap-rectangle>

Search-Box <!-- Search-Box -->
<el-amap-search-box class="search-box" :search-option="searchOption" :on-search-result="onSearchResult"></el-amap-search-box>
<el-amap vid="amapDemo">
</el-amap>
  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue-amap 是一个基于 Vue.js高德地图组件库,支持在线地图和离线地图。实现高德离线地图,需要先下载离线地图数据,在 Vue-amap 引入并配置离线地图数据即可。 以下是实现步骤: 1. 下载离线地图数据:在高德官网下载离线地图数据,下载后解压缩得到离线地图数据文件夹。 2. 在 Vue 项目安装 vue-amap: ``` npm install vue-amap --save ``` 3. 在 main.js 引入和配置 vue-amap: ``` import VueAMap from 'vue-amap'; Vue.use(VueAMap); VueAMap.initAMapApiLoader({ key: 'your amap key', plugin: ['AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.Geolocation'], // 默认高德 sdk 版本为 1.4.4 v: '1.4.4', // 配置离线地图数据路径,注意路径要使用绝对路径 uiVersion: '1.0', map: { // 配置离线地图数据路径,注意路径要使用绝对路径 mapStyle: 'amap://styles/whitesmoke', features: ['bg', 'road', 'building'], customMapStyle: { 'styleJson': [ { 'featureType': 'water', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'land', 'elementType': 'all', 'stylers': { 'color': '#f3f3f3' } }, { 'featureType': 'railway', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'highway', 'elementType': 'all', 'stylers': { 'color': '#fdfdfd' } }, { 'featureType': 'highway', 'elementType': 'labels', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'arterial', 'elementType': 'geometry', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'arterial', 'elementType': 'geometry.fill', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'poi', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'green', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'subway', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'manmade', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'local', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'arterial', 'elementType': 'labels', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'boundary', 'elementType': 'all', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'building', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'label', 'elementType': 'labels.text.fill', 'stylers': { 'color': '#999999' } } ] }, // 配置离线地图数据路径,注意路径要使用绝对路径 zooms: [3, 20], viewMode: '3D', expandZoomRange: true }, // 配置离线地图数据路径,注意路径要使用绝对路径 filePath: '/path/to/offline/map/data' }); ``` 注意,需要将 filePath 设置为离线地图数据文件夹的绝对路径。 4. 在组件使用 Vue-amap: ``` <template> <el-amap :zoom="13" :center="center"> <el-amap-marker :position="center"></el-amap-marker> </el-amap> </template> <script> export default { data () { return { center: [116.397428, 39.90923] }; } }; </script> ``` 在 el-amap 设置 zoom 和 center 即可显示地图。el-amap-marker 可以设置标记点。 至此,就完成了 Vue-amap 实现高德离线地图的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值