微信小程序开发之map地图实现教程


Posted in Javascript onJune 08, 2017

前言

微信小程序地图操作比较简单,api也很少,使用map组件来展示。说到地图,那就先来看基础定位:

定位用到wx.getLocation(OBJECT)函数,代码如下:

wx.getLocation({
 type: 'wgs84',
 success: function(res) {
 var latitude = res.latitude
 var longitude = res.longitude
 var speed = res.speed
 var accuracy = res.accuracy
 }
})

定位成功会返回四个参数值,如下:

微信小程序开发之map地图实现教程

map属性太多,先看一下:

微信小程序开发之map地图实现教程

如果用到地图,基本上所有属性都会用到。

下面一一看一下,我们先看效果图吧,先看真相:

微信小程序开发之map地图实现教程

这里我只用了一个markers,就是定位当前位置的红色markers,用法如下:

wx.getLocation({
  type: 'wgs84', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
  success: function (res) {

  _this.setData({
   latitude: res.latitude,
   longitude: res.longitude,
   markers: [{
   id: "1",
   latitude: res.latitude,
   longitude: res.longitude,
   width: 50,
   height: 50,
   iconPath: "/assests/imgs/my.png",
   title: "哪里"

   }],
   circles: [{
   latitude: res.latitude,
   longitude: res.longitude,
   color: '#FF0000DD',
   fillColor: '#7cb5ec88',
   radius: 3000,
   strokeWidth: 1
   }]

  })
  }

 })

这里加了circles,半径是3000米,具体的api可自行看官网。

接下来看看controls,控制层,在地图上显示控件,控件不随着地图移动,看API:

微信小程序开发之map地图实现教程

注意看示例图的右上角,有两个按钮,加减号,是控制地图scale的数值变化,动态缩放地图的,controls用法也很简单:

controls: [{
  id: 1,
  iconPath: '/assests/imgs/jian.png',
  position: {
  left: 320,
  top: 100 - 50,
  width: 20,
  height: 20
  },
  clickable: true
 },
 {
  id: 2,
  iconPath: '/assests/imgs/jia.png',
  position: {
  left: 340,
  top: 100 - 50,
  width: 20,
  height: 20
  },
  clickable: true
 }
 ]

最后我们看一张gif图:

微信小程序开发之map地图实现教程

最后上一下具体代码:

wxml:

<map id="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="{{scale}}" controls="{{controls}}" bindcontroltap="controltap" markers="{{markers}}" circles="{{circles}}" bindmarkertap="markertap" polyline="{{polyline}}" bindregionchange="regionchange" show-location style="width: 100%; height: {{view.Height}}px;"></map>

js:

Page({
 data: {
 Height: 0,
 scale: 13,
 latitude: "",
 longitude: "",
 markers: [],
 controls: [{
  id: 1,
  iconPath: '/assests/imgs/jian.png',
  position: {
  left: 320,
  top: 100 - 50,
  width: 20,
  height: 20
  },
  clickable: true
 },
 {
  id: 2,
  iconPath: '/assests/imgs/jia.png',
  position: {
  left: 340,
  top: 100 - 50,
  width: 20,
  height: 20
  },
  clickable: true
 }
 ],
 circles: []

 },

 onLoad: function () {
 var _this = this;

 wx.getSystemInfo({
  success: function (res) {
  //设置map高度,根据当前设备宽高满屏显示
  _this.setData({
   view: {
   Height: res.windowHeight
   }

  })



  }
 })

 wx.getLocation({
  type: 'wgs84', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
  success: function (res) {

  _this.setData({
   latitude: res.latitude,
   longitude: res.longitude,
   markers: [{
   id: "1",
   latitude: res.latitude,
   longitude: res.longitude,
   width: 50,
   height: 50,
   iconPath: "/assests/imgs/my.png",
   title: "哪里"

   }],
   circles: [{
   latitude: res.latitude,
   longitude: res.longitude,
   color: '#FF0000DD',
   fillColor: '#7cb5ec88',
   radius: 3000,
   strokeWidth: 1
   }]

  })
  }

 })

 },

 regionchange(e) {
 console.log("regionchange===" + e.type)
 },

 //点击merkers
 markertap(e) {
 console.log(e.markerId)

 wx.showActionSheet({
  itemList: ["A"],
  success: function (res) {
  console.log(res.tapIndex)
  },
  fail: function (res) {
  console.log(res.errMsg)
  }
 })
 },

 //点击缩放按钮动态请求数据
 controltap(e) {
 var that = this;
 console.log("scale===" + this.data.scale)
 if (e.controlId === 1) {
  // if (this.data.scale === 13) {
  that.setData({
  scale: --this.data.scale
  })
  // }
 } else {
  // if (this.data.scale !== 13) {
  that.setData({
  scale: ++this.data.scale
  })
  // }
 }


 },


})

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Javascript 相关文章推荐
JavaScript中void(0)的具体含义解释
Feb 27 Javascript
推荐40个简单的 jQuery 导航插件和教程(下篇)
Sep 14 Javascript
使用 Node.js 做 Function Test实现方法
Oct 25 Javascript
JavaScript常用脚本汇总(二)
Mar 04 Javascript
JavaScript中的toLocaleDateString()方法使用简介
Jun 12 Javascript
原生js实现autocomplete插件
Apr 14 Javascript
Angular2学习笔记——详解路由器模型(Router)
Dec 02 Javascript
JavaScript简单计算人的年龄示例
Apr 15 Javascript
JavaScript 完成注册页面表单校验的实例
Aug 19 Javascript
angular.js4使用 RxJS 处理多个 Http 请求
Sep 23 Javascript
Vue两种组件类型:递归组件和动态组件的用法
Aug 06 Javascript
JavaScript 生成唯一ID的几种方式
Feb 19 Javascript
详解AngularJS用Interceptors来统一处理HTTP请求和响应
Jun 08 #Javascript
微信小程序开发之实现自定义Toast弹框
Jun 08 #Javascript
微信小程序开发之toast等弹框提示使用教程
Jun 08 #Javascript
javascript实现二叉树遍历的代码
Jun 08 #Javascript
微信小程序tabbar不显示解决办法
Jun 08 #Javascript
javascript实现二叉树的代码
Jun 08 #Javascript
微信小程序搜索组件wxSearch实例详解
Jun 08 #Javascript
You might like
PHP 实现判断用户是否手机访问
2015/01/21 PHP
php+mysqli使用预处理技术进行数据库查询的方法
2015/01/28 PHP
php获取文章内容第一张图片的方法示例
2017/07/03 PHP
php使用pthreads v3多线程实现抓取新浪新闻信息操作示例
2020/02/21 PHP
jQuery动态添加 input type=file的实现代码
2012/06/14 Javascript
js 页面元素的几个用法总结
2013/11/18 Javascript
Javascript 赋值机制详解
2014/11/23 Javascript
js实现九宫格图片半透明渐显特效的方法
2015/02/16 Javascript
js实现YouKu的漂亮搜索框效果
2015/08/19 Javascript
JS实现的倒计时效果实例(2则实例)
2015/12/23 Javascript
学习JavaScript鼠标响应事件
2015/12/25 Javascript
window.onload绑定多个事件的两种解决方案
2016/05/15 Javascript
jQuery EasyUI结合zTree树形结构制作web页面
2017/09/01 jQuery
解决Vue编译时写在style中的路径问题
2017/09/21 Javascript
JS和Canvas实现图片的预览压缩和上传功能
2018/03/30 Javascript
微信小程序之判断页面滚动方向的示例代码
2018/08/30 Javascript
浅谈Layui的eleTree树式选择器使用方法
2019/09/25 Javascript
[02:50]【扭转乾坤,只此一招】DOTA2永雾林渊版本开启新篇章
2020/12/22 DOTA
SublimeText 2编译python出错的解决方法(The system cannot find the file specified)
2013/11/27 Python
举例讲解Python编程中对线程锁的使用
2016/07/12 Python
Django与JS交互的示例代码
2017/08/23 Python
Python有序查找算法之二分法实例分析
2017/12/11 Python
python 制作自定义包并安装到系统目录的方法
2018/10/27 Python
pyQT5 实现窗体之间传值的示例
2019/06/20 Python
pyqt 实现QlineEdit 输入密码显示成圆点的方法
2019/06/24 Python
python 怎样将dataframe中的字符串日期转化为日期的方法
2019/09/26 Python
Python如何在循环内使用list.remove()
2020/06/01 Python
Python如何向SQLServer存储二进制图片
2020/06/08 Python
GUESS德国官网:美国牛仔服装品牌
2017/02/14 全球购物
2014学年自我鉴定
2014/02/23 职场文书
市场总经理岗位职责
2014/04/11 职场文书
上班离岗检讨书
2014/09/10 职场文书
医院领导班子查摆问题对照检查材料思想汇报
2014/10/08 职场文书
年会主持人开场白台词
2015/05/29 职场文书
CentOS8.4安装Redis6.2.6的详细过程
2021/11/20 Redis
SQL Server中的游标介绍
2022/05/20 SQL Server