小程序地图详细位置,加距离计算

腾讯地图下载

 放入

 

wxml页面

<button bindtap="access">获取位置</button>
<!--form表单-->
<form bindsubmit="formSubmit">
    <label>终点坐标:
    <input style="border:1px solid #000;" name="dest"></input>
    </label>
    <!--提交表单数据-->
    <button form-type="submit">计算距离</button>
</form>
<!--渲染起点经纬度到终点经纬度距离,单位为米-->
<view wx:for="{{distance}}" wx:key="index">
    <view>起点到终点的步行距离为{{item}}米</view>
</view>
<map id="myMap"
    markers="{{markers}}"
    style="width:100%;height:300px;"
    longitude="{{poi.longitude}}"
    latitude="{{poi.latitude}}" scale='16' show-location>
</map>

js

// pages/word/word.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
 
// 实例化API核心类
var qqmapsdk = new QQMapWX({
    key: '自己的key' // 必填
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    res:'',
  },
  access(){
    var _this=this;
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        const latitude = res.latitude
        const longitude = res.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: '',//图标路径
              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
              },
              res:res.ad_info.city
            });
          },
        })
      },
      
     })
     
  },

  formSubmit(e) {
    console.log(e);
    var _this = this;
    qqmapsdk.geocoder({
      address: e.detail.value.dest, //传入地址(  address: '北京故宫', )
      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参数不填默认当前地址
          //获取表单提交的经纬度并设置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);
          }
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
百度地图API可以用于在小程序中进行距离计算。在使用百度地图API进行距离计算时,需要注意以下几点: 1. 数据格式:确保输入的经纬度数值正确,并与对应的坐标系一一对应。 2. 数据容量:个人账号有查询数量限制,建议一次性不要调用次数太多。 3. 坐标系:注意自己的数据是wgs84、gcj02或其他坐标系。 4. 公共交通一体化:百度地图查询公共交通路径时,一般将公交和地铁放在一起组合查询,可以得到公共交通的总出行距离,但无法得到公交和地铁各自的出行距离。 在小程序中使用百度地图API进行距离计算的示例代码如下: ```javascript <script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=百度地图密钥"></script> // 获取自身定位 var map = new BMap.Map("container"); var point = new BMap.Point(116.331398,39.897445); map.centerAndZoom(point,19); // 获取定位 var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ var mk = new BMap.Marker(r.point); map.addOverlay(mk); map.panTo(r.point); console.log('您的位置:', r.point.lng, ',', r.point.lat); } else { console.log('failed', this.getStatus()); } }) // 获取目标点定位 ``` 在wxml页面中,可以使用以下代码来实现百度地图API距离计算的功能: ```html <button bindtap="access">获取位置</button> <form bindsubmit="formSubmit"> <label>终点坐标: <input style="border:1px solid #000;" name="dest"></input></label> <button form-type="submit">计算距离</button> </form> <view wx:for="{{distance}}" wx:key="index"> <view>起点到终点的步行距离为{{item}}米</view> </view> <map id="myMap" markers="{{markers}}" style="width:100%;height:300px;" longitude="{{poi.longitude}}" latitude="{{poi.latitude}}" scale='16' show-location></map> ``` 以上是关于在小程序中使用百度地图API进行距离计算的一些示例代码和注意事项。希望对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值