温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序怎么实现在地图上显示自己的位置

发布时间:2022-01-13 15:31:05 来源:亿速云 阅读:1172 作者:iii 栏目:大数据

本篇内容主要讲解“微信小程序怎么实现在地图上显示自己的位置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“微信小程序怎么实现在地图上显示自己的位置”吧!

画面截图

微信小程序怎么实现在地图上显示自己的位置

index.wxml

<!--index.wxml-->

<view class="container">

 <map longitude="{{longitude}}" latitude="{{latitude}}" markers="{{markers}}" style=" width: 375px; height: 500px;"></map>

 <text class="angle_text">{{location}}</text>

</view>

内容很简单,画面上布置了一个map对象和text对象。

其中map对象分别指定了longitude,latitude和markers。相信你还记得:在双重花括号{{}}包围的部分是变量,它们的值在对应页面的js文件中定义。

index.js

//index.js

//获取应用实例

const app = getApp()

Page({

 data: {                     //数据定义

   longitude: 0,        // 对应wxml文件中的longitude变量

   latitude: 0,          // 对应wxml文件中的latitude变量

   location: ',',         // 对应wxml文件中的location变量

   markers: [{          // 对应wxml文件中的markers变量

     id: 0,

     latitude: 0,

     longitude: 0,

     width: 50,

     height: 50

   }],

 },

 onShow: function() {

   var that = this

   wx.getLocation({

     type: 'gcj02', // 返回 可以 用于 wx. openLocation 的 经纬度

     success: function (res) {

       var latitude = res.latitude

       var longitude = res.longitude

       console.log(res)

       var location = latitude.toFixed(2) + ',' + longitude.toFixed(2)

       that.setData({ longitude: longitude,

                      latitude: latitude,

                      location: location,

                      markers: [{latitude: latitude,

                                 longitude: longitude,

                               }]

                   });

     }

   })

 },

})

这段代码实现了生命周期函数onShow,它的核心是ws.getLocation,它的输出通过传递的success:function来处理。处理的内容很简单,就是通过setData函数设定到各个数据上。

到此,相信大家对“微信小程序怎么实现在地图上显示自己的位置”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI