微信小程序開發(fā)之map地圖實(shí)現(xiàn)教程
前言
微信小程序地圖操作比較簡(jiǎn)單,api也很少,使用map組件來(lái)展示。說到地圖,那就先來(lái)看基礎(chǔ)定位:
定位用到wx.getLocation(OBJECT)函數(shù),代碼如下:
wx.getLocation({
type: 'wgs84',
success: function(res) {
var latitude = res.latitude
var longitude = res.longitude
var speed = res.speed
var accuracy = res.accuracy
}
})
定位成功會(huì)返回四個(gè)參數(shù)值,如下:

map屬性太多,先看一下:

如果用到地圖,基本上所有屬性都會(huì)用到。
下面一一看一下,我們先看效果圖吧,先看真相:

這里我只用了一個(gè)markers,就是定位當(dāng)前位置的紅色markers,用法如下:
wx.getLocation({
type: 'wgs84', // 默認(rèn)為 wgs84 返回 gps 坐標(biāo),gcj02 返回可用于 wx.openLocation 的坐標(biāo)
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可自行看官網(wǎng)。
接下來(lái)看看controls,控制層,在地圖上顯示控件,控件不隨著地圖移動(dòng),看API:

注意看示例圖的右上角,有兩個(gè)按鈕,加減號(hào),是控制地圖scale的數(shù)值變化,動(dòng)態(tài)縮放地圖的,controls用法也很簡(jiǎn)單:
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圖:

最后上一下具體代碼:
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) {
//設(shè)置map高度,根據(jù)當(dāng)前設(shè)備寬高滿屏顯示
_this.setData({
view: {
Height: res.windowHeight
}
})
}
})
wx.getLocation({
type: 'wgs84', // 默認(rèn)為 wgs84 返回 gps 坐標(biāo),gcj02 返回可用于 wx.openLocation 的坐標(biāo)
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)
},
//點(diǎn)擊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)
}
})
},
//點(diǎn)擊縮放按鈕動(dòng)態(tài)請(qǐng)求數(shù)據(jù)
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
})
// }
}
},
})
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- 微信小程序 地圖定位簡(jiǎn)單實(shí)例
- 微信小程序 地圖(map)實(shí)例詳解
- 微信小程序中進(jìn)行地圖導(dǎo)航功能的實(shí)現(xiàn)方法
- 微信小程序 使用騰訊地圖SDK詳解及實(shí)現(xiàn)步驟
- 微信小程序 高德地圖SDK詳解及簡(jiǎn)單實(shí)例(源碼下載)
- 微信小程序 地圖map詳解及簡(jiǎn)單實(shí)例
- 微信小程序之獲取當(dāng)前位置經(jīng)緯度以及地圖顯示詳解
- 微信小程序教程之本地圖片上傳(leancloud)實(shí)例詳解
- 微信小程序地圖(map)組件點(diǎn)擊(tap)獲取經(jīng)緯度的方法
- 微信小程序map地圖使用方法詳解
相關(guān)文章
setTimeout 函數(shù)在前端延遲搜索實(shí)現(xiàn)中的作用詳解
這篇文章主要為大家介紹了setTimeout 函數(shù)在前端延遲搜索實(shí)現(xiàn)中的作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
詳談js中數(shù)組(array)和對(duì)象(object)的區(qū)別
下面小編就為大家?guī)?lái)一篇詳談js中數(shù)組(array)和對(duì)象(object)的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-02-02
利用百度echarts實(shí)現(xiàn)圖表功能簡(jiǎn)單入門示例【附源碼下載】
這篇文章主要介紹了利用百度echarts實(shí)現(xiàn)圖表功能簡(jiǎn)單,結(jié)合簡(jiǎn)單示例形式分析了echarts插件的圖標(biāo)繪制功能相關(guān)實(shí)現(xiàn)技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2019-06-06
javascript 獲取模態(tài)窗口的滾動(dòng)位置代碼
模態(tài)窗口的滾動(dòng)位置獲取辦法還有不知道的嗎?下面的方法或許對(duì)大家有所幫助,感興趣的朋友可以了解下,希望對(duì)大家有所幫助2013-08-08
JavaScript面向?qū)ο笕齻€(gè)基本特征實(shí)例詳解【封裝、繼承與多態(tài)】
這篇文章主要介紹了JavaScript面向?qū)ο笕齻€(gè)基本特征,結(jié)合實(shí)例形式詳細(xì)分析了JavaScript面向?qū)ο笕齻€(gè)基本特征封裝、繼承與多態(tài)的概念、原理、用法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
JS+CSS實(shí)現(xiàn)鼠標(biāo)經(jīng)過彈出一個(gè)DIV框完整實(shí)例(帶緩沖動(dòng)畫漸變效果)
這篇文章主要介紹了JS+CSS實(shí)現(xiàn)鼠標(biāo)經(jīng)過彈出一個(gè)DIV框的實(shí)現(xiàn)方法,帶緩沖漸變動(dòng)畫效果,涉及鼠標(biāo)事件的響應(yīng)及結(jié)合時(shí)間函數(shù)定時(shí)觸發(fā)形成動(dòng)畫漸變效果的相關(guān)技巧,需要的朋友可以參考下2016-03-03
完美解決input[type=number]無(wú)法顯示非數(shù)字字符的問題
下面小編就為大家?guī)?lái)一篇完美解決input[type=number]無(wú)法顯示非數(shù)字字符的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-02-02
JavaScript實(shí)現(xiàn)頁(yè)面電子時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)頁(yè)面電子時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06

