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

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

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

环境搭建

注册,获取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)
    • 相关文章
    • 微信模板消息改版后发送规则记录(微信订阅消息参数值内容限制说明)(1个评论)
    • 微信支付v3对接所需工具及命令(0个评论)
    • 2023年9月1日起:微信小程序必须备案才能上线运营(0个评论)
    • 腾讯官方客服回应了:微信好友上限约10000个!(1个评论)
    • 2023年做微信小程序的老铁注意:新增收费项、微信小程序获取手机号也收费了(2个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    Top

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

    侯体宗的博客