在微信小程序中加载高德地图

 地图插件:卓伙地图组件

示例地址:pages/gdmap

一、申请卓伙地图插件

 插件申请地址:卓伙地图组件

二、声明插件

app.json

  "plugins": {
    "zhgeo-plugin": {
      "version": "latest",
      "provider": "wx3fe0bd34d4615f50"
    }

三、在page内引入插件

pages/gdmap/gdmap.json 

{
  "usingComponents": {
    "v-zhgeo": "plugin://zhgeo-plugin/zhgeo-component"
  }
}

pages/gdmap/gdmap.wxml

<!--pages/gdmap/gdmap.wxml-->
<view class="container">
    <v-zhgeo id="gd-leafletwx" />
</view>

pages/gdmap/gdmap.js

// pages/gdmap/gdmap.js
const plugin = requirePlugin('zhgeo-plugin')
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  showLocation() {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const container = this.selectComponent('#gd-leafletwx')
    let min_zoom = 10
    let max_zoom = 18
    container.initLeafletMap(function(L) {
      wx.leaflet ??= {}
      wx.leaflet.L = L;
      var map = L.map(container, {
        minZoom: min_zoom,
        maxZoom: max_zoom,
      });

      var geoJsonData = {
        "type": "FeatureCollection", 
        "features": [
          { "type": "Feature", "id":"1", "properties": { "address": "2"   }, "geometry": { "type": "Point", "coordinates": [120.625348,31.294888] } },
          { "type": "Feature", "id":"2", "properties": { "address": "151" }, "geometry": { "type": "Point", "coordinates": [120.625675,31.294898] } },
          { "type": "Feature", "id":"3", "properties": { "address": "21"  }, "geometry": { "type": "Point", "coordinates": [120.625375,31.294636] } },
          { "type": "Feature", "id":"4", "properties": { "address": "14"  }, "geometry": { "type": "Point", "coordinates": [120.626067,31.294609] } },
          { "type": "Feature", "id":"5", "properties": { "address": "38B" }, "geometry": { "type": "Point", "coordinates": [120.627379,31.296556] } },
          { "type": "Feature", "id":"6", "properties": { "address": "38"  }, "geometry": { "type": "Point", "coordinates": [120.623645,31.293604] } },
          { "type": "Feature", "id":"7", "properties": { "address": "39"  }, "geometry": { "type": "Point", "coordinates": [120.626008,31.294435] } },
          { "type": "Feature", "id":"8", "properties": { "address": "40"  }, "geometry": { "type": "Point", "coordinates": [120.625680,31.294315] } }
        ]
      };
      L.tileLayer('http://webrd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}', { subdomains: "1234" }).addTo(map)
      
      var markers = L.markerClusterGroup();
      
      var geoJsonLayer = L.geoJson(geoJsonData, {
        src: 'https://cdn.bootcdn.net/ajax/libs/leaflet/1.9.4/images/marker-icon-2x.png',
        onEachFeature: function (feature, layer) {
          layer.bindPopup(feature.properties.address);
        }
      })
      markers.addLayer(geoJsonLayer);

      map.addLayer(markers);
      map.fitBounds(markers.getBounds());
      
      markers.on('clusterclick', function (a) {
        // a.layer is actually a cluster
        console.log('cluster ' + a.layer.getAllChildMarkers().length);
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})

四、高德地图加载结果

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在微信小程序引入高德地图,可以按照以下步骤进行操作: 1.在高德地图开放平台上注册账号并创建应用,获取应用的Key。 2.在微信小程序开发者工具,找到app.json文件,在"permission"字段添加"scope.userLocation"。 3.在需要使用地图的页面的wxml文件添加以下代码: ```html <view class="map"> <map id="myMap" longitude="113.324520" latitude="23.099994" markers="{{markers}}" show-location bindtap="bindMapTap"></map> </view> ``` 其,longitude和latitude分别为地图的心点经纬度坐标,markers为标记点数组,show-location表示显示定位按钮,bindtap表示绑定地图点击事件。 4.在对应页面的js文件引入高德地图SDK并初始化地图: ```javascript const amapFile = require('../../libs/amap-wx.js'); Page({ data: { markers: [{ iconPath: "../../images/marker.png", id: 0, latitude: 23.099994, longitude: 113.324520, width: 50, height: 50 }] }, onLoad: function () { var that = this; var myAmapFun = new amapFile.AMapWX({key: 'your amap key'}); myAmapFun.getRegeo({ success: function(data){ //成功回调 console.log(data); that.setData({ markers: [{ iconPath: "../../images/marker.png", id: 0, latitude: data[0].latitude, longitude: data[0].longitude, width: 50, height: 50 }] }) }, fail: function(info){ //失败回调 console.log(info); } }) }, bindMapTap: function(){ //地图点击事件 console.log('map tap'); } }) ``` 其,amap-wx.js是高德地图提供的微信小程序SDK,需要下载并放在libs文件夹下。初始化地图时,需要传入之前获取的应用Key,在onLoad函数调用getRegeo方法获取当前定位信息,并将标记点设置为当前定位点。 5.在小程序管理后台,进入开发设置页面,将请求域名列表添加以下域名: ``` https://restapi.amap.com ``` 以上就是在微信小程序引入高德地图的基本步骤,具体的使用可以根据需求进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

卓伙

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

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

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

打赏作者

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

抵扣说明:

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

余额充值