微信小程序开发之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调用XML制作连动下拉列表框
Jun 25 Javascript
js 自制滚动条的小例子
Mar 16 Javascript
jquery实现点击消失的代码
Mar 03 Javascript
Js操作树节点自动折叠展开的几种方法
May 05 Javascript
JS显示日历和天气的方法
Mar 01 Javascript
Bootstrap的modal拖动效果
Dec 25 Javascript
jQuery之动画ajax事件(实例讲解)
Jul 18 jQuery
全面介绍vue 全家桶和项目实例
Dec 27 Javascript
JS声明对象时属性名加引号与不加引号的问题及解决方法
Feb 16 Javascript
js html实现计算器功能
Nov 13 Javascript
vue实现设置载入动画和初始化页面动画效果
Oct 28 Javascript
Javascript使用integrity属性进行安全验证
Nov 07 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
PHP4实际应用经验篇(5)
2006/10/09 PHP
PHP中的strtr函数使用介绍(str_replace)
2011/10/20 PHP
浅谈php serialize()与unserialize()的用法
2013/06/05 PHP
php实现点击可刷新验证码
2015/11/07 PHP
XHProf报告字段含义的解析
2016/05/17 PHP
PHP 匿名函数与注意事项详细介绍
2016/11/26 PHP
PHP函数积累总结
2019/03/19 PHP
php常用的工具开发整理
2019/09/26 PHP
PHP pthreads v3在centos7平台下的安装与配置操作方法
2020/02/21 PHP
TFDN图片播放器 不错自动播放
2006/10/03 Javascript
JavaScript setTimeout使用闭包功能实现定时打印数值
2015/12/18 Javascript
浅谈javascript中的加减时间
2016/07/12 Javascript
基于js里调用函数时,函数名带括号和不带括号的区别
2016/07/28 Javascript
浅析Jquery操作select
2016/12/13 Javascript
微信小程序中实现一对多发消息详解及实例代码
2017/02/14 Javascript
javascript 中Cookie读、写与删除操作
2017/03/29 Javascript
详解Js中的模块化是如何实现的
2017/10/18 Javascript
jQuery超简单遮罩层实现方法示例
2018/09/06 jQuery
详解基于webpack&amp;gettext的前端多语言方案
2019/01/29 Javascript
小程序云开发之用户注册登录
2019/05/18 Javascript
详解element-ui中表单验证的三种方式
2019/09/18 Javascript
python求解数组中两个字符串的最小距离
2018/09/27 Python
Django安装配置mysql的方法步骤
2018/10/15 Python
Lacoste美国官网:经典POLO衫品牌
2016/10/12 全球购物
德国知名健康零食网上商店:Seeberger
2017/07/27 全球购物
维多利亚的秘密官方旗舰店:VICTORIA’S SECRET
2018/04/02 全球购物
创业计划书如何吸引他人眼球
2014/01/10 职场文书
舞蹈比赛获奖感言
2014/02/04 职场文书
制冷与空调专业毕业生推荐信
2014/07/07 职场文书
领导干部对照检查材料
2014/08/24 职场文书
三好生演讲稿
2014/09/12 职场文书
行政后勤人员工作计划应该怎么写?
2019/08/16 职场文书
新手入门Mysql--sql执行过程
2021/06/20 MySQL
javascript数组includes、reduce的基本使用
2021/07/02 Javascript
nginx服务器的下载安装与使用详解
2021/08/02 Servers
Java面试题冲刺第十八天--Spring框架3
2021/08/07 面试题