Vue3-使用高德地图,遮罩、自定义图标、点击图标放大、同时获取当地天气

1.进入高德开放平台

高德开放平台 | 高德地图API

2.控制台-我的应用-创建新应用-添加key

点击提交后就会生成Key 

 3.在项目中下载

yarn  add  @amap/amap-jsapi-loader

4.在需要的页面引入

import AMapLoader from "@amap/amap-jsapi-loader";
/*在Vue3中使用时,需要引入Vue3中的shallowRef方法(使用shallowRef进行非深度监听,
因为在Vue3中所使用的Proxy拦截操作会改变JSAPI原生对象,所以此处需要区别Vue2使用方式对地图对象进行非深度监听,
否则会出现问题,建议JSAPI相关对象采用非响应式的普通对象来存储)*/
import { shallowRef } from "@vue/reactivity";

5.生成地图,添加遮罩

 经纬度定位查询:经纬度查询定位 拾取坐标系统 经纬度查询地图

const initMap = () => {
  window._AMapSecurityConfig = {
    securityJsCode: "申请下来的securityJsCode",
  };
  AMapLoader.load({
    key: "申请下来的key", // 申请好的Web端开发者Key,首次调用 load 时必填
    version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
    plugins: [
      "AMap.ToolBar",
      "AMap.Driving",
      "AMap.Polygon",
      "AMap.DistrictSearch",
      "AMap.Object3D",
      "AMap.Polyline",
      "AMap.Map",
      "AMap.Marker",
      () => {
        map.addControl(new AMap.ToolBar()); // 工具条控件;范围选择控件
        map.addControl(new AMap.Scale()); // 显示当前地图中心的比例尺
        map.addControl(new AMap.HawkEye()); // 显示缩略图
        map.addControl(new AMap.Geolocation()); // 定位当前位置
        map.addControl(new AMap.MapType()); // 实现默认图层与卫星图,实时交通图层之间切换
        map.addControl(new AMap.Object3D());
        map.addControl(new AMap.Polyline());
        map.addControl(new AMap.Marker());
      },
    ], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  })
    .then((AMap) => {
      AMaps.value = AMap;
      var district = new AMap.DistrictSearch(opts);
      district.search("填写地名", function (status, result) {
        const bounds = result.districtList[0].boundaries;
        const mask = []; //遮罩
        for (var i = 0; i < bounds.length; i += 1) {
          mask.push([bounds[i]]);
        }

        map.value = new AMap.Map("container", {
          mask: mask,
          center: ["填写经纬度"],
          viewMode: "3D",//模式
          showLabel: true,
          labelzIndex: 130,
          pitch: 40,
          zoom: 10,
          layers: [
            new AMap.TileLayer.RoadNet({
              //rejectMapMask:true
            }),
            new AMap.TileLayer.Satellite(),
          ],
        });
}
})

6.自定义图标,点击图标放大

//直接写在initMap()中
//maskerList为图标的经纬度,名称,图标数组

        let maskerIn;
        maskerList.forEach((item) => {
          maskerIn = new AMap.Marker({
            position: item.position,
            icon: new AMap.Icon({
              // 图标尺寸
              size: new AMap.Size(32, 32),
              // 图标的取图地址
              // image: '../../assets/imgs/map-landmark.png',
              image: item.icon,
              // 图标所用图片大小
              imageSize: new AMap.Size(32, 32),
              // 图标取图偏移量
              imageOffset: new AMap.Pixel(0, 0),
            }),
            offset: new AMap.Pixel(-13, -30),
            label: {
              content: `<div style="background: rgba(7, 7, 7, 0.8)  !important;">${item.name}</div>`,
            },
            map: map.value,
          });
          maskerIn.on("click", (e) => {
            markerClick(e, item.name);
          });
        });
        map.value.add(maskerIn); //添加到地图
//点击放大地图,图标,label
const markerClick = (e, name) => {
  map.value.setZoomAndCenter(11, e.target.getPosition()); // 点击放大
  isOpen.value = true; // 打开dialog
  self.selectedMarkerStore = e.target.getExtData();
  //判断是第否点击过
  if (self.lastClickedMarker) {
    let icon = self.lastClickedMarker.getIcon();
    self.lastClickedMarker.setOffset(new AMap.Pixel(-10, -30));
    self.lastClickedMarker.setIcon(
      new AMap.Icon({
        size: new AMap.Size(32, 32), // 图标尺寸
        image: icon._opts.image, // Icon的图像
        imageSize: new AMap.Size(32, 32),
      })
    );
    self.lastClickedMarker.setLabel({
      content: `<div style="background: rgba(7, 7, 7, 0.8) !important;">${name}</div>`,
    });
  }
  let icon2 = e.target.getIcon();
  console.log(e.target.getTitle());
  e.target.setIcon(
    new AMap.Icon({
      size: new AMap.Size(65, 65), // 图标尺寸
      image: icon2._opts.image, // Icon的图像
      imageSize: new AMap.Size(65, 65),
    })
  );
  e.target.getLabel();
  e.target.setLabel({
    content: `<div  style="line-height:50px; font-size: 19px;
  font-family: Source Han Sans SC-Medium, Source Han Sans SC;
  font-weight: 500; padding: 10px;">${name}</div>`,
  });
  // e.target.setOffset(new AMap.Pixel(-18, -50))
  //有点击就给他加进去这样就能获取上一次数据
  self.lastClickedMarker = e.target;
};

7.获取当地天气

// 获取天气
// 填写需要获取天气的地点名称
const getWeather = () => {
  window._AMapSecurityConfig = {
    securityJsCode: "申请下来的securityJsCode",
  };
  AMapLoader.load({
    key: "申请下来的key", // 申请好的Web端开发者Key,首次调用 load 时必填
    version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  }).then((AMap) => {
    AMap.plugin("AMap.CitySearch", function () {
      var citySearch = new AMap.CitySearch();
      citySearch.getLocalCity(function (status, result) {
        //console.log(result.city, "city");
        if (status === "complete" && result.info === "OK") {
          AMap.plugin("AMap.Weather", function () {
            var weather = new AMap.Weather();
            //执行实时天气信息查询
            weather.getLive("填写地名", function (err, data) {
              //console.log("天气", data);
              localStorage.setItem("weather", JSON.stringify(data));
            });
          });
        }
      });
    });
  });
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue3组合式API中使用vue-baidu-map-3x自定义地图中心点,可以按照以下步骤: 1. 安装vue-baidu-map-3x 你可以通过以下命令使用npm安装vue-baidu-map-3x: ``` npm install vue-baidu-map-3x --save ``` 2. 在组合式API中引入vue-baidu-map-3x 在需要使用百度地图的组件中,你需要先引入vue-baidu-map-3x。例如,在一个名为Map.vue的组件中,你可以这样引入: ``` <script> import { defineComponent } from 'vue' import BaiduMap from 'vue-baidu-map-3x' export default defineComponent({ components: { BaiduMap }, setup() { // 组合式API的代码 } }) </script> ``` 3. 在setup函数中定义地图中心点 在setup函数中,你可以定义地图的中心点和缩放级别。例如: ``` <script> import { defineComponent } from 'vue' import BaiduMap from 'vue-baidu-map-3x' export default defineComponent({ components: { BaiduMap }, setup() { const center = { lng: 116.404, lat: 39.915 } const zoom = 12 return { center, zoom } } }) </script> ``` 在这个例子中,我们定义了地图的中心点为北京市中心,缩放级别为12。 4. 在模板中使用地图 最后,在模板中使用vue-baidu-map-3x组件,并且传入定义的中心点和缩放级别。例如: ``` <template> <div class="map-container"> <baidu-map :center="center" :zoom="zoom"></baidu-map> </div> </template> ``` 现在,你已经在Vue3组合式API中使用vue-baidu-map-3x自定义了地图的中心点。你可以通过修改center的值来改变地图的中心点。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值