以下是微信小程序使用腾讯地图的代码示例:
1. 在小程序的app.json文件中添加腾讯地图SDK的引用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | { "pages" : [ "pages/index/index" ], "window" : { "navigationBarTitleText" : "Demo" }, "plugins" : { "myPlugin" : { "version" : "1.0.0" , "provider" : "wxidxxxxxxxxxxxxxxxx" , "requiredLibraries" : [ "libA" , "libB" ] }, "tencentMap" : { "version" : "1.0.0" , "provider" : "wxidxxxxxxxxxxxxxxxx" } } } |
2. 在小程序页面的js文件中引入腾讯地图SDK
1 2 3 4 | const QQMapWX = require( '../../utils/qqmap-wx-jssdk.min.js' ); const qqmapsdk = new QQMapWX({ key: 'your_key' }); |
3. 在小程序页面的wxml文件中添加地图组件
1 | < map id = "map" longitude = "113.324520" latitude = "23.099994" scale = "14" show-location></ map id = "map" longitude = "113.324520" latitude = "23.099994" scale = "14" show-location> |
4. 在小程序页面的js文件中使用腾讯地图SDK提供的接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | Page({ data: { markers: [] }, onLoad: function () { qqmapsdk.search({ keyword: '酒店' , success: (res) => { const markers = res.data.map((item) => ({ id: item.id, latitude: item.location.lat, longitude: item.location.lng, title: item.title, iconPath: '/images/marker.png' })); this .setData({ markers }); } }); } }); |