当前位置:   article > 正文

小程序地图的使用笔记_小程序 getsuggestion

小程序 getsuggestion
这两天在看小程序的地图,写写笔记记录一下

小程序官方文档提供的几种位置方法
小程序的官方文档也有提供几个方法(这几个方法很简单,看看就知道了),但是一般都满足不了~~

腾讯地图提供的jssdk
根据提示使用腾讯地图jssdk需要申请,在实例化的时候填入密匙,接下来就可以使用他提供的各种方法了
这里写图片描述

我先说说我做关键词搜索和点击搜索结果进行路线规划(规划目前可能只是按自驾的路线,不完善的地方麻烦大家赐教了)

搜索我先使用getLocation获取当前位置坐标

wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        latitude = res.latitude
        longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        console.log('Complete')
      }
    }) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

接着利用获取到的城市坐标获取城市名字

qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function(res){
        //console.log(res)
        cityName = res.result.address_component.city
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log('获取当前城市完成');
      }
    })
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

然后调用getSuggestion方法进行当前城市的关键词搜索显示功能(将获取到的关键词循环显示在搜索下面,给便利出来的每个选项加个点击事件)

qqmapsdk.getSuggestion({
      keyword: inputData,
      region: cityName,
      success: function (res) {
        let searchData = res.data
        that.setData({searchPlace: searchData})
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

点击所选地名进行地名的坐标存储

placeChoose: function (options){
    let location = options.currentTarget.dataset.location
    wx.setStorageSync('location', location)
    wx.navigateBack({
      // 返回的页面数
      data: 1
    })
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

进行线路规划(画路线)

//前往搜索地
  goSearchPlace: function(){
    let that = this
    let searchLat = wx.getStorageSync('location').lat
    let searchLon = wx.getStorageSync('location').lng
    wx.request({
      url: 'https://apis.map.qq.com/ws/direction/v1/driving/?from=24.488476,118.096247&to=' + searchLat + ',' + searchLon + '&output=json&callback=cb&key=22VBZ-REEK5-WVSI7-QKCOP-QPM6E-W7BPO',
      success: function (res) {
        coors = res.data.result.routes[0].polyline
        for (var i = 2; i < coors.length; i++) {
          coors[i] = coors[i - 2] + coors[i] / 1000000
        }
        that.project()
      }
    })
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

画线路函数我单独抽出来

project: function(){
    var b=[]
    for (var i=0;i<coors.length;i=i+2){
      b[i / 2] = {
        latitude: coors[i], longitude: coors[i + 1]
      }
      console.log(b[i / 2])
    }
    console.log(b.length)
    that2.setData({
      polyline: [{
        points: b,
        color: "#9999FF",
        width: 4,
        dottedLine: false
      }]
    })
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

这里写图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/495248
推荐阅读
相关标签
  

闽ICP备14008679号