How to get address by wechat applet

Time:2021-2-20

This example for you to share the wechat applet to get the address of the specific code, for your reference, the specific content is as follows

wxml

<map longitude="{{data.longitude}}" latitude="{{data.latitude}}" scale="14" markers="{{markers}}" polyline="{{polyline}}" show-location style="width: 100%; height: 500px;"></map>
<text>
 {{data.longitude}}
 {{data.latitude}}
 {{address}}
</text>
<! -- longitude of longitude center -- >
<! -- latitude of latitude center -- >
<! -- scale level, value range is 3-20 -- >
<! -- markers marker points -- >

js

var QQMapWX = require('../js/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
 data:{
  data:"",
  str:"",
  address:""
 },
 onLoad: function () {
  let _this = this;
  //Instantiating API core classes
  qqmapsdk = new QQMapWX({
   key: 'key'
  });
  //Promise get address
  let gets = new Promise((resolve, reject) => {
   wx.getLocation({
    type: 'wgs84',
    success:resolve
   })
  }).then(function(res){
   console.log(res)
   _this.setData({
    data:res
   })
  })

  //Get address in normal way
  wx.getLocation({
   type: 'wgs84',
   success:function(res){
    _this.setData({
     str: _this.res
    })
    console.log(res)
    var demo = new QQMapWX({
     key: 'key'
    });
    //Address resolution
    demo.reverseGeocoder ({// address resolution
     location: {
      latitude: res.latitude,
      longitude: res.longitude
     },
     Success: function (RES) {// to specific address
      console.log(res);
      //Get the address
      _this.setData({
       address: res.result.address
      })
     },
     fail: function (res) {
      console.log(res);
     }
    });
   }
  })
 },

I recommend a wechat applet tutorial with high degree of attention for you: wechat applet development tutorial is carefully arranged for you. I hope you like it.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.