微信小程序实现定位(小白专用)

微信小程序实现定位(小白专用)微信小程序实现定位需要引入腾讯地图的 api 腾讯地图下载地址创建 libs 文件夹引入地图 js 在需要定位的页面写 js 引入地图 varMapWX require libs map wx jssdk js varmapsdk Page onLoad function 实例化 API 核心类 mapsdk newMapWX key 申请的 key

微信小程序实现定位

需要引入腾讯地图的api

腾讯地图下载地址

创建libs文件夹引入地图js在这里插入图片描述
在需要定位的页面写js
//引入地图 var MapWX = require('../../libs/map-wx-jssdk.js'); var mapsdk; 
Page({ 
    onLoad: function () { 
    // 实例化API核心类 mapsdk = new MapWX({ 
    key: '申请的key' }); }, onShow: function () { 
    // 调用接口 mapsdk.search({ 
    keyword: '口袋机', success: function (res) { 
    console.log(res); }, fail: function (res) { 
    console.log(res); }, complete: function (res) { 
    console.log(res); } }); }) 
重点核心代码
// 判断用户是否拒绝地理位置信息授权,拒绝的话重新请求授权 getUserLocation: function () { 
    let that = this; wx.getSetting({ 
    success: (res) => { 
    console.log(JSON.stringify(res)) // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面 // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权 // res.authSetting['scope.userLocation'] == true 表示 地理位置授权 if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) { 
    wx.showModal({ 
    title: '请求授权当前位置', content: '需要获取您的地理位置,请确认授权', success: function (res) { 
    if (res.cancel) { 
    wx.showToast({ 
    title: '拒绝授权', icon: 'none', duration: 1000 }) } else if (res.confirm) { 
    wx.openSetting({ 
    success: function (dataAu) { 
    if (dataAu.authSetting["scope.userLocation"] == true) { 
    wx.showToast({ 
    title: '授权成功', icon: 'success', duration: 1000 }) //再次授权,调用wx.getLocation的API that.getLocation(); } else { 
    wx.showToast({ 
    title: '授权失败', icon: 'none', duration: 1000 }) } } }) } } }) } else if (res.authSetting['scope.userLocation'] == undefined) { 
    //调用wx.getLocation的API that.getLocation(); } else { 
    //调用wx.getLocation的API that.getLocation(); } } }) }, // 获取定位当前位置的经纬度 getLocation: function () { 
    let that = this; wx.getLocation({ 
    type: 'wgs84', success: function (res) { 
    // console.log('res2', res) let latitude = res.latitude let longitude = res.longitude app.globalData.lat = res.latitude; app.globalData.lng = res.longitude; //把onload定位时候的经纬度存到全局 let speed = res.speed let accuracy = res.accuracy; that.getLocal(latitude, longitude) }, fail: function (res) { 
    console.log('fail' + JSON.stringify(res)) } }) }, // 获取当前地理位置 getLocal: function (latitude, longitude) { 
    let that = this; mapsdk.reverseGeocoder({ 
    location: { 
    latitude: latitude, longitude: longitude }, success: function (res) { 
    // console.log('res3', res)  // 保存一下当前定位的位置留着后面重新定位的时候搜索附近地址用 // app.globalData.currentLocation = district; that.setData({ 
    address: res.result.address, district: res.result.ad_info.district, street: res.result.address_component.street, rough: res.result.formatted_addresses.rough }) }, fail: function (res) { 
    console.log(res); }, complete: function (res) { 
    // console.log(res); } }); }, 
data保存地理位置信息
 data: { 
    address: '', district: '', street: '', rough: '' }, 
html数据绑定
<view class="head"> <image src="../../images/dizhi.png"> 
     image> <text class="color">{ 
   {district}} 
      text> <text class="color">{ 
    {street}} 
       text> <text class="color">{ 
     {rough}} 
        text>  
         view> 
效果展示

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211492.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午10:18
下一篇 2026年3月18日 下午10:18


相关推荐

  • macOS 安装lrzsz

    macOS 安装lrzszmac自带的终端是不支持lrzsz的,需要下载安装iterm2MacOS终端利器iTerm2安装homebrewbrew的MAC安装macOS安装lrzsz进入/usr/local/bin由于原作者下架了项目,我们只能手动新建脚本了,下面请无脑全部复制即可。1.创建iterm2-recv-zmodem.sh#!/bin/bashosascript-e…

    2022年6月23日
    136
  • Windows 64位下安装Redis 以及 可视化工具Redis Desktop Manager的安装和使用

    Windows 64位下安装Redis 以及 可视化工具Redis Desktop Manager的安装和使用Windows64 位下安装 Redis 一 Redis 介绍 Redis 是一个 key value 存储系统 和 Memcached 类似 它支持存储的 value 类型相对更多 包括 string 字符串 list 链表 set 集合 zset sortedset 有序集合 和 hash 哈希类型 这些数据类型都支持 push pop add remove 及取交集并集和差集及更丰富的操作 而

    2026年3月26日
    2
  • Vue(5)计算属性computed

    Vue(5)计算属性computed前言一般情况下属性都是放到data中的,但是有些属性可能是需要经过一些逻辑计算后才能得出来,那么我们可以把这类属性变成计算属性。比如以下:<divid="example&quot

    2022年7月29日
    9
  • 2013年蓝桥杯真题[通俗易懂]

      1.某人年龄的立方是4位数,年龄的四次方是6位数,这10位数包含0到9,每个恰好出现一次,求他年龄多大publicclassOne{publicstaticvoidmain(String[]args){for(inti=10;i<100;i++){inti1=i*i*i;int…

    2022年4月12日
    46
  • 向量积计算三角形面积

    向量积计算三角形面积向量积 数学中又称外积 叉积 物理中称矢积 叉乘 是一种在向量空间中向量的二元运算 向量积可以被定义为 模长 在这里 表示两向量之间的夹角 共起点的前提下 0 180 它位于这两个矢量所定义的平面上 向量积的模 长度 在数值上等于 及其夹角 组成的平行四边形的面积 所以求三角形 ABC 的面积 根据向量积的意义 可得三角形 ABC 的面积 S a b ay

    2026年3月18日
    3
  • VM无法与SecureCRTPortable.exe 相连接不上时

    VM无法与SecureCRTPortable.exe 相连接不上时1.IP地址已经配置成功2.打开后打开后连接不上3.点击更改设置还原默认设置4.在重新将子网ip改为192.168.100.05.这样就OK了

    2022年6月5日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号