微信小程序-地图实例

效果图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
第一步申请高德地图Key
申请地址
下载amap-wx.js 高德地图封装好了的API

用到API接口二次封装

let amapFile = require("./amap-wx");
// 高德开发者key
let key = '申请的key';
let myAmapFun = new amapFile.AMapWX({ key });

class Amap {
  /**
   * 获取POI数据
   * @param {string} querykeywords 
   */
  static getPoiAround(querykeywords = '') {
    return new Promise((resolve, reject) => myAmapFun.getPoiAround({ querykeywords, success: resolve, fail: reject }));
  }

  /**
   * 获取地理描述数据
   */
  static getRegeo() {
    return new Promise((resolve, reject) => myAmapFun.getRegeo({ success: resolve, fail: reject }));
  }
  /**
   * 获取天气数据
   *
   */
  static getWeather() {
    return new Promise((resolve, reject) => myAmapFun.getWeather({ success: resolve, fail: reject }));
  }

  /**
   * 获取输入提示词
   * @param {string} keywords 
   * @param {string} location 
   */
  static getInputtips(city, location = '', keywords = '') {
    return new Promise((resolve, reject) => myAmapFun.getInputtips({ keywords, location, city, success: resolve, fail: reject }));
  }

  /**
   * 获取路线规划
   * type : getDrivingRoute --驾车
   * type : getWalkingRoute --步行
   * type : getTransitRoute --公交
   * type : getRidingRoute --骑行
   * @param {string} origin 
   * @param {string} destination 
   */
  static getRoute(origin, destination, type, city) {
    return new Promise((resolve, reject) => myAmapFun[type]({ origin, destination, city, success: resolve, fail: reject }));
  }
}

module.exports = Amap;

amap.getRegeo()调用获取地图描述,显示当前自己坐标点

    amap.getRegeo()
      .then(d => {
        console.log(d);
        let {
          name,
          desc,
          latitude,
          longitude
        } = d[0];
        //得到自己的坐标点(模拟器上不准确,准确请到手机体验)
        let {
          city
        } = d[0].regeocodeData.addressComponent;
        //获取详细城市地址
        var value = "查询附近信息的内容"
        //var lonlat = longitude + "," + latitude;
        //使用高德地图API getPoiAround 获取周边信息,参数value
        amap.getPoiAround(value)
          .then(d => {
          //成功回调 markers地图显示坐标点
            let {
              markers
            } = d;
            //给每一个坐标点添加图标标识
            markers.forEach(item => {
              item.iconPath = "/images/marker.png";
            })
            that.setData({
              poisData:d.poisData,
              markers
            });
            that.showMarkerInfo(markers[0]);
            that.changeMarkerColor(0);
          })
          .catch(e => {
            console.log(e);
          })

        this.setData({
          city,
          latitude,
          longitude,
          textData: {
            name,
            desc
          }
        })
      })
      .catch(e => {
        console.log(e);
      })

经度(longitude)、纬度(latitude)
使用小程序wx.getSystemInfo API获取手机屏幕高度 设置地图大小
得到坐标点,渲染数据
controls 增加地图标识作用回到自己坐标
bindcontroltap 绑定事件

  clickcontrol(e) {
    console.log("回到用户当前定位点");
    let {
      controlId
    } = e;
    //创建地图实例
    //使用moveToLocation方法,将地图中心移动到当前定位点,需要配合map组件的show-location使用
    let mpCtx = wx.createMapContext("map");
    mpCtx.moveToLocation();
  },

scale 地图显示大小
markers 坐标点
bindmarkertap 绑定坐标点 点击事件

    wx.getSystemInfo({
      success:function(res)
      {
        console.log(res)
        that.setData({
          height:res.windowHeight
        })
      }
    })
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员-南

你的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值