環(huán)境搭建
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了西城免費(fèi)建站歡迎大家使用!
注冊(cè),獲取APPID(沒有這個(gè)不能真雞調(diào)試)
下載微信web開發(fā)者工具(挺多bug,將就用)
打開微信web開發(fā)者工具,掃碼登錄,新建小程序,輸入APPID,勾選創(chuàng)建quick start項(xiàng)目。
工程結(jié)構(gòu)
可以看到工程根目錄中有個(gè)app.js,這里可以定義全局變量,通過getApp()獲取。
項(xiàng)目中有了一些示例,已經(jīng)有了獲取用戶信息的方法等。
開發(fā)地圖定位,選擇位置功能
我們直接修改index頁面來做這個(gè)功能。
準(zhǔn)備
新建imgs目錄,加入2個(gè)圖標(biāo)(ic_location和ic_position),用于標(biāo)記當(dāng)前位置,和地圖中央位置。
添加定位功能
修改app.js,加入定位功能,獲取當(dāng)前位置。
//app.js App({ onLaunch: function () { //調(diào)用API從本地緩存中獲取數(shù)據(jù) 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{ //調(diào)用登錄接口 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', // 默認(rèn)為 wgs84 返回 gps 坐標(biāo),gcj02 返回可用于 wx.openLocation 的坐標(biāo) 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標(biāo)簽,如下
<map id="map4select" longitude="{{longitude}}" latitude="{{latitude}}" markers="{{markers}}" scale="20" bindregionchange="regionchange" controls="{{controls}}"> </map>
需要給地圖指定一個(gè)id,后面可以通過id獲取地圖的上下文。
監(jiān)聽bindregionchange事件,地圖變化的時(shí)候可以監(jiān)聽到。
地圖的大小不要寫死,動(dòng)態(tài)設(shè)置,我這里打算設(shè)置為寬高都是屏幕寬度。
controls是固定在map組件上面的。一開始我想用image替代,但是設(shè)置z-index也不能在地圖上面,畢竟不是H5開發(fā)。
邏輯代碼編寫
編輯index.js
var app = getApp() Page({ data:{ map_width: 380 ,map_height: 380 } //show current position ,onLoad: function(){ var that = this; // 獲取定位,并把位置標(biāo)示出來 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 // 動(dòng)態(tài)設(shè)置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 }] }) } }) } //獲取中間點(diǎn)的經(jīng)緯度,并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) { // 地圖發(fā)生變化的時(shí)候,獲取中間點(diǎn),也就是用戶選擇的位置 if(e.type == 'end'){ this.getLngLat() } } ,markertap(e) { console.log(e) } })
展示
這樣,就OK啦,用戶可以看到自己的定位,如果覺得有偏差,可以移動(dòng)地圖,把中央點(diǎn)放到自己認(rèn)為的準(zhǔn)確位置上。
新聞標(biāo)題:微信小程序開發(fā)之map地圖組件定位并手動(dòng)修改位置偏差
文章起源:http://www.chinadenli.net/article12/ishhgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、做網(wǎng)站、網(wǎng)站設(shè)計(jì)、定制網(wǎng)站、品牌網(wǎng)站制作、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
營(yíng)銷型網(wǎng)站建設(shè)知識(shí)