侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

微信小程序开发之map地图组件定位并手动修改位置偏差

微信(小程序)  /  管理员 发布于 3年前   530

环境搭建

注册,获取APPID(没有这个不能真鸡调试)

下载微信web开发者工具(挺多bug,将就用)

打开微信web开发者工具,扫码登录,新建小程序,输入APPID,勾选创建quick start项目。

工程结构

可以看到工程根目录中有个app.js,这里可以定义全局变量,通过getApp()获取。

项目中有了一些示例,已经有了获取用户信息的方法等。

开发地图定位,选择位置功能

我们直接修改index页面来做这个功能。

准备

新建imgs目录,加入2个图标(ic_location和ic_position),用于标记当前位置,和地图中央位置。

ic_locationic_position

添加定位功能

修改app.js,加入定位功能,获取当前位置。

//app.jsApp({ onLaunch: function () {  //调用API从本地缓存中获取数据  var logs = wx.getStorageSync('logs') || []  logs.unshift(Date.now())  wx.setStorageSync('logs', logs) } ,getUserInfo:function(cb){  var that = this  if(this.globalData.userInfo){   typeof cb == "function" && cb(this.globalData.userInfo)  }else{   //调用登录接口   wx.login({    success: function () {     wx.getUserInfo({      success: function (res) {       that.globalData.userInfo = res.userInfo       typeof cb == "function" && cb(that.globalData.userInfo)      }     })    }   })  } } //get locationInfo ,getLocationInfo: function(cb){  var that = this;  if(this.globalData.locationInfo){    cb(this.globalData.locationInfo)  }else{    wx.getLocation({     type: 'gcj02', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标     success: function(res){      that.globalData.locationInfo = res;      cb(that.globalData.locationInfo)     },     fail: function() {      // fail     },     complete: function() {      // complete     }    })  } } ,globalData:{  userInfo:null  ,locationInfo: null }})

地图控件布局

修改pages/index/index.wxml文件,添加map标签,如下

需要给地图指定一个id,后面可以通过id获取地图的上下文。

监听bindregionchange事件,地图变化的时候可以监听到。

地图的大小不要写死,动态设置,我这里打算设置为宽高都是屏幕宽度。

controls是固定在map组件上面的。一开始我想用image替代,但是设置z-index也不能在地图上面,毕竟不是H5开发。

逻辑代码编写

编辑index.js

var app = getApp()Page({  data:{   map_width: 380   ,map_height: 380  }  //show current position  ,onLoad: function(){  var that = this;  // 获取定位,并把位置标示出来  app.getLocationInfo(function(locationInfo){    console.log('map',locationInfo);    that.setData({     longitude: locationInfo.longitude     ,latitude: locationInfo.latitude     ,markers:[      {      id: 0      ,iconPath: "../../imgs/ic_position.png"      ,longitude: locationInfo.longitude      ,latitude: locationInfo.latitude      ,width: 30      ,height: 30      }     ]    })  })  //set the width and height  // 动态设置map的宽和高  wx.getSystemInfo({   success: function(res) {    console.log('getSystemInfo');    console.log(res.windowWidth);    that.setData({      map_width: res.windowWidth     ,map_height: res.windowWidth     ,controls: [{      id: 1,      iconPath: '../../imgs/ic_location.png',      position: {       left: res.windowWidth/2 - 8,       top: res.windowWidth/2 - 16,       width: 30,       height: 30      },      clickable: true     }]    })   }  }) } //获取中间点的经纬度,并mark出来 ,getLngLat: function(){   var that = this;   this.mapCtx = wx.createMapContext("map4select");   this.mapCtx.getCenterLocation({    success: function(res){      that.setData({      longitude: res.longitude      ,latitude: res.latitude      ,markers:[       {       id: 0       ,iconPath: "../../imgs/ic_position.png"       ,longitude: res.longitude       ,latitude: res.latitude       ,width: 30       ,height: 30       }      ]     })    }   }) } ,regionchange(e) {  // 地图发生变化的时候,获取中间点,也就是用户选择的位置   if(e.type == 'end'){     this.getLngLat()   } } ,markertap(e) {  console.log(e) }})

展示

这样,就OK啦,用户可以看到自己的定位,如果觉得有偏差,可以移动地图,把中央点放到自己认为的准确位置上。

这里写图片描述
这里写图片描述
这里写图片描述

您可能感兴趣的文章:

  • 微信小程序 高德地图路线规划实现过程详解
  • 微信小程序map组件结合高德地图API实现wx.chooseLocation功能示例
  • 微信小程序 高德地图SDK详解及简单实例(源码下载)
  • 微信小程序JS加载esmap地图的实例详解
  • 微信小程序获取位置展示地图并标注信息的实例代码
  • 微信小程序 腾讯地图SDK 获取当前地址实现解析
  • 微信小程序基于高德地图查找位置并显示文字


  • 上一条:
    微信小程序移动拖拽视图-movable-view实例详解
    下一条:
    微信公众号生成新浪短网址的实现(快速生成)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 2023年9月1日起:微信小程序必须备案才能上线运营(0个评论)
    • 腾讯官方客服回应了:微信好友上限约10000个!(0个评论)
    • 2023年做微信小程序的老铁注意:新增收费项、微信小程序获取手机号也收费了(0个评论)
    • 小程序开发之跳转微信直播示例api(0个评论)
    • 在uni_app中开发小程序之常用功能示例代码汇总(0个评论)
    • 近期文章
    • 在Nuxt3中如何减少import model声明及初始化加载models快速开发秘诀(0个评论)
    • 适用于 Laravel 的表单请求测试程序包(0个评论)
    • Laravel 10.25版本发布(0个评论)
    • 在go语言中实现生成文件的校验和功能代码示例(0个评论)
    • Laravel 11版本抢先看,看将有什么新功能发布(0个评论)
    • goose数据库迁移工具介绍及使用流程步骤(0个评论)
    • 中国程序员“翻墙”为海外软件公司打工,105.8万工资被罚没!转知乎(0个评论)
    • 在go语言gin框架中使用Sharding(Gorm分表中间件)实现分表流程步骤(0个评论)
    • 在PHP提高性能方式之开启OPCache扩展及OPCache配置参数详解(0个评论)
    • 在js的websocket客户端开发中遇到代码割裂情况解决方案(0个评论)
    • 近期评论
    • 路人 在

      php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..
    • 博主 在

      科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 @ mashrdn 多切换几个节点测试,免费ssr是没那么稳..
    • mashrdn 在

      科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 V2rayn免费节点添加上去了,youtobe无法打开网页,是怎么回事..
    • 张伟 在

      科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 3q!有用,不过免费节点隔天就要去git上复制新的导进去..
    • 博主 在

      科学上网翻墙访问Google , 上外网神器佛跳墙VPN(永久免费)使用流程步骤中评论 该篇教程已不能用了,告知大家,免的老有老铁问我!..
    • 2016-10
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    • 2023-06
    • 2023-07
    • 2023-08
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客