麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 課堂 > 小程序 > 正文

微信小程序開發之map地圖組件定位并手動修改位置偏差

2020-03-21 15:15:55
字體:
來源:轉載
供稿:網友

環境搭建

注冊,獲取APPID(沒有這個不能真雞調試)

下載微信web開發者工具(挺多bug,將就用)

打開微信web開發者工具,掃碼登錄,新建小程序,輸入APPID,勾選創建quick start項目。

工程結構

可以看到工程根目錄中有個app.js,這里可以定義全局變量,通過getApp()獲取。 

項目中有了一些示例,已經有了獲取用戶信息的方法等。

開發地圖定位,選擇位置功能

我們直接修改index頁面來做這個功能。

準備

新建imgs目錄,加入2個圖標(ic_location和ic_position),用于標記當前位置,和地圖中央位置。

微信小程序,map,地圖組件,定位 微信小程序,map,地圖組件,定位

添加定位功能

修改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標簽,如下

<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>

需要給地圖指定一個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,地圖組件,定位 
微信小程序,map,地圖組件,定位 
微信小程序,map,地圖組件,定位


注:相關教程知識閱讀請移步到微信小程序開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品视频一区二区三区四区国 | 黄色免费在线网站 | 国产一级淫片免费看 | 蜜桃精品视频在线观看 | 狠狠干狠狠操 | 中文字幕精品久久 | 91久久久久久久 | 欧美成人精品一级 | 黄色的视频免费观看 | 日本精品中文字幕 | 精品xxxx户外露出视频 | 免费毛片在线 | 成人一区二区在线观看视频 | 欧美a级大胆视频 | 久久蜜桃香蕉精品一区二区三区 | 欧美日韩在线免费观看 | 久国产精品视频 | 久久精品视频12 | 永久av在线免费观看 | 亚洲第一成人在线 | 成年人免费视频播放 | 国产精品视频免费在线观看 | 久草成人在线 | 国产二区三区四区 | 人禽l交免费视频观看 视频 | 久久久久一区二区三区四区五区 | 成人福利视频网站 | 激情综合婷婷久久 | 亚洲国产精久久久久久久 | 成人男女啪啪免费观看网站四虎 | 色七七亚洲 | 午夜爽爽爽男女免费观看hd | 免费观看视频在线 | 成人午夜免费在线视频 | 日韩美香港a一级毛片免费 欧美一级淫片007 | 美女污污在线观看 | 激情在线视频 | 国产精品美女一区二区 | 美女黄色毛片免费看 | 精品视频一区二区三区四区 | 福利在线免费 |