温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序如何开发MAP

发布时间:2021-07-05 11:37:36 来源:亿速云 阅读:150 作者:小新 栏目:web开发

这篇文章主要介绍了微信小程序如何开发MAP,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

微信小程序 开发MAP(地图)实例详解

在创建MAP(地图)前,请各位小伙伴们认真的去了解微信小程序开发的说明。

https://mp.weixin.qq.com/debug/wxadoc/dev/component/map.html#map

了解完MAP(地图)里的属性之后,接下来我们就来创建一个简单的MAP(地图)控件。

第一步:肯定是创建项目、起项目名、项目地址

PS:我这里以index的文件为名

第二步:我们来写 index.wxml 文件的代码

WXML文件代码:

<map id="map4select" longitude="{{longitude}}" 
latitude="{{latitude}}" markers="{{markers}}"
 scale="20"  
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({
  success: 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 ,
    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: 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文件夹里面,小图标的路径引用正确就可以显示出来。

感谢你能够认真阅读完这篇文章,希望小编分享的“微信小程序如何开发MAP”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI