小程序引入腾讯地图API 完整过程

var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');  //lib 文件夹里是腾讯封装好的api 直接去官网下载解压

//用户点击导航按钮之后一系列的骚操作

goMap(){

    var that = this;

//判断用户手机系统有没有开启定位服务

wx.getSystemInfo({

  success(res) {

  var isopendingwei = res.locationEnabled;

  if(isopendingwei==false){

  wx.showToast({

    title: '请开启手机定位功能!',

    icon: 'none',

    duration: 2000

  })

  }else{

    // 手机开启定位了

    wx.showLoading({

    title: '正在获取位置...',

  })

  setTimeout(()=>{

    // const vm = this

//判断小程序有没有获取定位权限 没有授权的情况用getSetting打开授权

    wx.getSetting({   

//如果已经获取定位权限 就可以开始定位咯

      success: (res)=> {     

        // 1. scope.userLocation 为真, 代表用户已经授权

        if (res.authSetting['scope.userLocation']) {

          // 使用 JavaScript SDK 获取目的地经纬度

          // 实例化API核心类

        let  qqmapsdk = new QQMapWX({

              key: '腾讯地图申请的key'

          });


  // reverseGeocoder 为 QQMapWX 解析 经纬度的方法

  const latitude=Number(that.data.parkDetailData.latitude)

  const longitude=Number(that.data.parkDetailData.longitude)

  qqmapsdk.reverseGeocoder({

      location: { latitude,longitude},

      success:(res)=> {

          that.setData({

              ad_info: res.result.ad_info,

              city: `${res.result.address_component.province}${res.result.formatted_addresses.rough}`

          })

          console.log(res,"?看看解析成啥了")

          qqmapsdk.geocoder({

            // 写入目的地名称

              address: `${that.data.city}`,

              success:  (res)=> {

                  console.log(res);

                  var local = res.result.location;

                  that.setData({

                      latitude: local.lat,

                      longitude: local.lng

                  })

              }

          })

        // 1.1 使用 getlocation 获取用户 经纬度位置

        wx.getLocation({

            success:(res)=>{

                // 1.2 获取用户位置成功后,将会返回 latitude, longitude 两个字段,代表用户的经纬度位置

                console.log(res)

                // 1.3 将获取到的 经纬度传值给 getAddress 解析出 具体的地址

                wx.openLocation({

                  latitude: Number(that.data.latitude),

                  longitude: Number(that.data.longitude),

                  scale: 28,

                  // 这里也要写目的地信息

                  name: `${that.data.city}`, //打开后显示的地址名称

              })

            }

        })

      }

  })

    }else {

            // 2. 用户未授权的情况下,可能会包含用户第一次授权时点击取消,就不能使用getSetting,要用openSetting来获取授权

            wx.openSetting({

                success:(res)=> {

                    // 2.1 如果二次授权允许了 userLocation 权限, 就再次执行获取位置的接口

                    if (res.authSetting["scope.userLocation"]) {

          // 使用 JavaScript SDK 获取目的地经纬度

          // 实例化API核心类

          let  qqmapsdk = new QQMapWX({

            key: '腾讯地图申请的key'

        });


// reverseGeocoder 为 QQMapWX 解析 经纬度的方法

const latitude=Number(that.data.parkDetailData.latitude)

const longitude=Number(that.data.parkDetailData.longitude)

qqmapsdk.reverseGeocoder({

    location: { latitude,longitude},

    success:(res)=> {

        that.setData({

            ad_info: res.result.ad_info,

            city: res.result.address

        })

        // console.log(this.data.ad_info,this.data.city,"?看看解析成啥了")

        qqmapsdk.geocoder({

          // 写入目的地名称

            address: `${that.data.city}`,

            success:  (res)=> {

                console.log(res);

                var local = res.result.location;

                that.setData({

                    latitude: local.lat,

                    longitude: local.lng

                })

            }

        })

      // 1.1 使用 getlocation 获取用户 经纬度位置

      wx.getLocation({

          success:(res)=>{

              // 1.2 获取用户位置成功后,将会返回 latitude, longitude 两个字段,代表用户的经纬度位置

              console.log(res)

              // 1.3 将获取到的 经纬度传值给 getAddress 解析出 具体的地址

              wx.openLocation({

                latitude: Number(that.data.latitude),

                longitude: Number(that.data.longitude),

                scale: 28,

                // 这里也要写目的地信息

                name: `${that.data.city}`, //打开后显示的地址名称

            })

          }

      })

    }

})

                    }

                }

            })

        }

      }

    })

  },2000)

  }

  }

}) 

  },

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。