使用腾讯位置服务计算两点距离
微信小程序JavaScript SDK | 腾讯位置服务
把下载好的包放在工具文件下

在.js中引入并实例化
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');// 实例化API核心类
var qqmapsdk = new QQMapWX({key: ' XXXXXX ' // 必填
});
表单测试
起点到终点的步行距离为{{item}}米
data: {distance:''},formSubmit(e){var _this = this;//调用地址解析接口qqmapsdk.geocoder({address: e.detail.value.dest, //传入终点的地址success: function (res) {console.log(res);var path = res.result.location; //终点坐标(经纬度)var strLocation = path.lat + ',' + path.lng;//拼接//调用距离计算接口qqmapsdk.calculateDistance({//mode: 'driving',//可选值:'driving'(驾车)、'walking'(步行),不填默认:'walking',可不填mode:'walking',//获取表单提交的经纬度并设置from和to参数(示例为string格式)from: '', //若起点有数据则采用起点坐标,若为空默认当前地址to: strLocation, //终点坐标success: function (res) { //成功后的回调console.log(res);var res = res.result;var dis = [];for (var i = 0; i < res.elements.length; i++) {dis.push(res.elements[i].distance); //将返回数据存入dis数组,}_this.setData({ //设置并更新distance数据distance: dis});},fail: function (error) {console.error(error);},complete: function (res) {console.log(res);}});}})},
当前地图展示
{markers[0].title}} -->
{{city}}
▼
data{city:'上海市松江区',markers:'',
}getMap(){var _this=thisvar latitude=wx.getStorageSync('latitude')var longitude=wx.getStorageSync('longitude')qqmapsdk.reverseGeocoder({location: {latitude: latitude,longitude: longitude},success: function(res) {//成功后的回调console.log(res);var res = res.result;var mks = [];mks.push({ // 获取返回结果,放到mks数组中title: res.address,id: 0,latitude: res.location.lat,longitude: res.location.lng,iconPath: '/images/home-s.png',//图标路径width: 20,height: 20,callout: { //在markers上展示地址名称,根据需求是否需要content: res.address,color: '#000',display: 'ALWAYS'}});_this.setData({ //设置markers属性和地图位置poi,将结果在地图展示markers: mks,poi: {latitude: res.location.lat,longitude: res.location.lng},city:_this.city});},})},//选择位置changeLoc(){wx.chooseLocation({success:res=>{var city = res.namethis.setData({city})wx.setStorageSync('latitude', res.latitude)wx.setStorageSync('longitude', res.longitude)this.getMap() }})},getLoc(){var that = this;wx.getLocation({type: 'wgs84',success (res) {const latitude = res.latitudeconst longitude = res.longitudewx.setStorageSync('latitude', latitude)wx.setStorageSync('longitude', longitude)}})},/*** 生命周期函数--监听页面加载*/onLoad: function (options) {this.getMap()},
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
