vue 中引入高德地图,自定义信息窗体,点标记

(参考官方文档:高德地图

1.引入

在index.html中引入

  <script src="https://webapi.amap.com/maps?v=1.4.1&key=0d78256ea89beeb8c25d1cd047549d1f"></script>
  <script src="https://webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>

2.页面中添加高德地图

// html
<div id="map-area"></div>
// js 在mounted里面进行
	data() {
	    return {
	    mapMarker: [ // 点标记数组
	    {
	    address:'上海市徐汇区小木桥路470号',
	    id: "d4378a71945248fe8ab4e1f3af20628e",
		latitudeGD: 31.19331,
		level: "市级",
		longitudeGD: 121.460609,
		name: "上海市公共法律服务中心",
		url: "http://sh.12348.gov.cn/sites/12348/zxspace/org-detail.jsp?entityId=d4378a71945248fe8ab4e1f3af20628e"
		},
		{
		address: "南丹东路60号",
		id: "d4378a718fc14dd996b83bb7f1ea1d46",
		latitudeGD: 31.190249,
		level: "区级",
		longitudeGD: 121.444415,
		name: "徐汇区公共法律服务中心",
		url: "http://sh.12348.gov.cn/sites/12348/zxspace/org-detail.jsp?entityId=d4378a718fc14dd996b83bb7f1ea1d46"
		}
	    ],
	   }
	},
  mounted() {
    this.setMapChart();
  }
  methods:{
  setMapChart() {
      // 创建地图实例
      this.map = new AMap.Map('map-area', {
        zoom: 13, // 级别
        zooms: [8, 18],
        resizeEnable: true,// 是否监控地图容器尺寸变化
        mapStyle: "amap://styles/blue", // 地图主题
      });
      // 以下是各类方法,光初始化上面代码就行
      this.infoWindow = new AMap.InfoWindow({
        isCustom: true,  //使用自定义窗体
        content: this.createInfoWindow('服务区', ''),
        offset: new AMap.Pixel(16, -25)
      });
      // zoom变化时,触发方法
      this.map.on('zoomend', ()=> {
        let zoom = this.map.getZoom()
        // console.log('zoom', zoom)
        if(zoom > 11) {
          // console.log('超过了')
          this.setMarker(this.mapMarker)
        }else {
          this.map.remove(this.allMarker)
        }
      });
      this.setCity('310104')
    },
      // 设置地图当前行政区
    setCity(Location) {
      let that = this;
      this.map.setCity(Location, ()=> {
        this.map.setZoom(14);
      });
    },
    // 设置地图marker
    setMarker(markerList) {
      this.map.remove(this.allMarker)
      this.allMarker= []
      markerList.map((e) => {
        var marker = new AMap.Marker({
          position: new AMap.LngLat(e.longitudeGD, e.latitudeGD),
          icon: this.markerIcon,
          offset: new AMap.Pixel(-13, -30)
        });
        marker.name = e.name;
        marker.url = e.url
        marker.on('mouseover', e => {
          this.infoWindow.setContent(this.createInfoWindow(e.target.name, e.target.url));
          // console.log('markerMouseOver',e)
          this.infoWindow.open(this.map, e.target.getPosition());
        });
        marker.on('mouseout', e => {
          this.map.clearInfoWindow();
        });
        marker.on('click', e => {
          console.log(111, e);
          if(e.lnglat) {
            console.log(222);
            window.open(e.target.url);
          }
        });
        marker.emit('click', { target: marker });
       
        this.allMarker.push(marker)
      })
       // 将 markers 添加到地图
        this.map.add(this.allMarker);
    },
    // 自定义信息窗体
    createInfoWindow(title, url) {
      let info = '<a style="display: block;font-size: .18rem;background-color: rgba(30,105,204,.8);color: #DDEBF5;padding: .15rem;border-radius: .04rem;text-decoration:none;" href="' + url + '" target="_blank">'+ title +'</a>'
        return info;
    },
  }

3.页面效果

点标记和信息窗体都可以点击
点标记和信息窗体都可以点击

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Vue使用高德地图自定义标记可以通过以下步骤实现: 1. 在Vue项目安装高德地图JavaScript API: ``` npm install --save @amap/amap-jsapi-loader ``` 2. 在Vue组件引入高德地图JavaScript API: ```javascript import AMapLoader from '@amap/amap-jsapi-loader'; export default { name: 'Map', data() { return { map: null, markers: [] }; }, mounted() { AMapLoader.load({ key: 'your_amap_key', version: '2.0', plugins: ['MarkerClusterer'], AMapUI: { version: '1.1', plugins: ['overlay/SimpleMarker'] } }).then((AMap) => { this.map = new AMap.Map('map-container', { zoom: 13, center: [116.397428, 39.90923] }); }); } } ``` 3. 创建自定义标记: ```javascript const marker = new AMap.Marker({ position: [116.397428, 39.90923], icon: new AMap.Icon({ size: new AMap.Size(25, 34), image: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png', imageSize: new AMap.Size(25, 34) }), offset: new AMap.Pixel(-13, -34), title: '北京市' }); ``` 4. 将标记添加到地图上: ```javascript this.markers.push(marker); this.map.add(marker); ``` 5. 完整代码: ```javascript <template> <div id="map-container"></div> </template> <script> import AMapLoader from '@amap/amap-jsapi-loader'; export default { name: 'Map', data() { return { map: null, markers: [] }; }, mounted() { AMapLoader.load({ key: 'your_amap_key', version: '2.0', plugins: ['MarkerClusterer'], AMapUI: { version: '1.1', plugins: ['overlay/SimpleMarker'] } }).then((AMap) => { this.map = new AMap.Map('map-container', { zoom: 13, center: [116.397428, 39.90923] }); const marker = new AMap.Marker({ position: [116.397428, 39.90923], icon: new AMap.Icon({ size: new AMap.Size(25, 34), image: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png', imageSize: new AMap.Size(25, 34) }), offset: new AMap.Pixel(-13, -34), title: '北京市' }); this.markers.push(marker); this.map.add(marker); }); } } </script> ``` 以上代码可以在Vue创建一个自定义标记并将其添加到高德地图上。可以根据需要调整标记的位置、图标、大小、偏移量和标题等属性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值