网络编程
位置:首页>> 网络编程>> JavaScript>> 微信小程序如何获取地址

微信小程序如何获取地址

作者:曳猫、  发布时间:2024-07-08 20:55:06 

标签:微信小程序,获取地址

本文实例为大家分享了微信小程序获取地址的具体代码,供大家参考,具体内容如下

微信小程序如何获取地址

wxml


<map id="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 中心经度 -->
<!-- latitude 中心纬度 -->
<!-- scale 缩放级别,取值范围为3-20 -->
<!-- markers 标记点 -->

js


var QQMapWX = require('../js/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
data:{
 data:"",
 str:"",
 address:""
},
onLoad: function () {
 let _this = this;
 // 实例化API核心类
 qqmapsdk = new QQMapWX({
  key: 'key'
 });
 // Promise 获取地址
 let gets = new Promise((resolve, reject) => {
  wx.getLocation({
   type: 'wgs84',
   success:resolve
  })
 }).then(function(res){
  console.log(res)
  _this.setData({
   data:res
  })
 })

// 正常方式获取地址
 wx.getLocation({
  type: 'wgs84',
  success:function(res){
   _this.setData({
    str: _this.res
   })
   console.log(res)
   var demo = new QQMapWX({
    key: 'key'
   });
   // 解析地址
   demo.reverseGeocoder({//地址解析
    location: {
     latitude: res.latitude,
     longitude: res.longitude
    },
    success: function (res) {//转为具体地址
     console.log(res);
     //获得地址
     _this.setData({
      address: res.result.address
     })
    },
    fail: function (res) {
     console.log(res);
    }
   });
  }
 })
},

为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。

来源:https://blog.csdn.net/weixin_45936690/article/details/103660373

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com