微信小程序 开发MAP(地图)实例详解
网络编程 2021-07-04 18:32www.168986.cn编程入门
这篇文章主要介绍了微信小程序 开发MAP(地图)实例详解的相关资料,需要的朋友可以参考下
微信小程序 开发MAP(地图)实例详解
在创建MAP(地图)前,请各位小伙伴们认真的去了解微信小程序开发的说明。
https://mp.weixin.qq./debug/wxadoc/dev/ponent/map.html#map
了解完MAP(地图)里的属性之后,接下来我们就来创建一个简单的MAP(地图)控件。
第一步肯定是创建项目、起项目名、项目地址
PS:我这里以index的文件为名
第二步我们来写 index.wxml 文件的代码
WXML文件代码
<map id="map4select" longitude="{{longitude}}" latitude="{{latitude}}" markers="{{markers}}" scale="20" style="width:{{map_width}}px;height:{{map_height}}px" bindregionchange="regionchange" controls="{{controls}}"> </map>
WXML文件的代码写好之后,就要来进行第三步了。
第三步写 index.js 文件的代码
var app = getApp() Page({ data: { map_width: 380 , map_height: 380 } //show current position , onLoad: function (options) { console.log(options.schedule_id); var that = this; // 获取定位,并把位置标示出来 that.setData({ longitude: 113.324520 , latitude: 23.099994 , markers: [ { id: 0 , iconPath: "../imgs/ic_position.png" , longitude: 113.324520 , latitude: 23.099994 , width: 30 , height: 30 } ] }) //set the width and height // 动态设置map的宽和高 wx.getSystemInfo({ suess: function (res) { 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 , : res.windowWidth / 2 - 16 , width: 30, height: 30 }, clickable: true }] }) } }) } //获取中间点的经纬度,并mark出来 , getLngLat: function () { var that = this; this.mapCtx = wx.createMapContext("map4select"); this.mapCtx.getCenterLocation({ suess: function (res) { that.setData({ longitude: 113.324520 , latitude: 23.099994 , markers: [ { id: 0 , iconPath: "../imgs/ic_position.png" , longitude: 113.324520 , latitude: 23.099994 , width: 30 , height: 30 } ] }) } }) } , regionchange(e) { // 地图发生变化的时候,获取中间点,也就是用户选择的位置 if (e.type == 'end') { this.getLngLat() } } , markertap(e) { console.log(e) } })
index.js 和 index.wxml 两个文件的代码已经写好,那么我们就来页面上看看效果。
PS“../imgs/ic_position.png” 和 “../imgs/ic_location.png” 是我在项目里创建的一个名叫imgs文件夹里面的两个定位小图标,各位小伙伴们可以根据自己的需求改换小图标,只需要把小图标放进imgs文件夹里面,小图标的路径引用正确就可以显示出来。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇:微信小程序 蓝牙的实现实例代码
下一篇:微信小程序商品到详情的实现
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指