uniapp地圖組件(map)使用與遇到的一些問題總結(jié)
前言
這段時間在開發(fā)uniapp的時候使用到map組件
總結(jié)一下本次在uniapp中使用map遇到的一些問題
文章分別是基礎(chǔ) 定位圖標 獲取自身經(jīng)緯度 通過經(jīng)緯度獲取當時城市信息
首先先看成品

廢話不多說,直接開始。
首先引入map組件
<template>
<view class="content">
<map style="width: 100%; height: 90vh;" :layer-style='5' :style="{height:mapheight}" :show-location='true' :latitude="latitude" :longitude="longitude" :markers="marker" :scale="scale" @markertap="markertap" @callouttap='callouttap'>
</map>
</view>
</template>
<script>
export default {
data() {
return {
latitude:23.106574, //緯度
longitude:113.324587,//經(jīng)度
scale:13,//縮放級別
bottomData:false,
marker: [
{
id:0,
latitude: 23.13065,//緯度
longitude: 113.3274,//經(jīng)度
iconPath: '', //顯示的圖標
rotate:0, // 旋轉(zhuǎn)度數(shù)
width:20, //寬
height:30, //高
// title:'我在這里',//標注點名
alpha:0.5, //透明度
callout:{//自定義標記點上方的氣泡窗口 點擊有效
content:'天寶大廈',//文本
color:'#ffffff',//文字顏色
fontSize:14,//文本大小
borderRadius:15,//邊框圓角
borderWidth:'10',
bgColor:'#e51860',//背景顏色
display:'ALWAYS',//常顯
},
},
{
id:1234597,
latitude: 23.106574,//緯度
longitude: 113.324587,//經(jīng)度
iconPath: '', //顯示的圖標
rotate:0, // 旋轉(zhuǎn)度數(shù)
width:20, //寬
height:30, //高
// title:'我在這里',//標注點名
alpha:0.5, //透明度
// label:{//為標記點旁邊增加標簽 //因背景顏色H5不支持
// color:'red',//文本顏色
// },
callout:{//自定義標記點上方的氣泡窗口 點擊有效
content:'廣州塔',//文本
color:'#ffffff',//文字顏色
fontSize:14,//文本大小
borderRadius:15,//邊框圓角
borderWidth:'10',
bgColor:'#e51860',//背景顏色
display:'ALWAYS',//常顯
},
},
{
id:2,
latitude: 23.1338,//緯度
longitude: 113.33052,//經(jīng)度
iconPath: '', //顯示的圖標
rotate:0, // 旋轉(zhuǎn)度數(shù)
width:20, //寬
height:30, //高
alpha:0.5, //透明度
callout:{//自定義標記點上方的氣泡窗口 點擊有效
content:'德隆大廈',//文本
color:'#ffffff',//文字顏色
fontSize:14,//文本大小
borderRadius:15,//邊框圓角
borderWidth:'10',
bgColor:'#e51860',//背景顏色
display:'ALWAYS',//常顯
},
},
{
id:3,
latitude: 23.136455,//緯度
longitude: 113.329002,//經(jīng)度
iconPath: '', //顯示的圖標
rotate:0, // 旋轉(zhuǎn)度數(shù)
width:20, //寬
height:30, //高
alpha:0.5, //透明度
callout:{//自定義標記點上方的氣泡窗口 點擊有效
content:'羊城國際商貿(mào)中心',//文本
color:'#ffffff',//文字顏色
fontSize:14,//文本大小
borderRadius:15,//邊框圓角
borderWidth:'10',
bgColor:'#e51860',//背景顏色
display:'ALWAYS',//常顯
},
},
{
id:4,
latitude: 23.224781,//緯度
longitude: 113.293911,//經(jīng)度
iconPath: '', //顯示的圖標
rotate:0, // 旋轉(zhuǎn)度數(shù)
width:20, //寬
height:30, //高
alpha:0.5, //透明度
callout:{//自定義標記點上方的氣泡窗口 點擊有效
content:'天瑞廣場A座',//文本
color:'#ffffff',//文字顏色
fontSize:16,//文本大小
borderRadius:15,//邊框圓角
borderWidth:'12',
bgColor:'#e51860',//背景顏色
display:'ALWAYS',//常顯
},
},
{
id:5,
latitude: 23.072726,//緯度
longitude: 113.277921,//經(jīng)度
iconPath: '', //顯示的圖標
rotate:0, // 旋轉(zhuǎn)度數(shù)
width:20, //寬
height:30, //高
alpha:0.5, //透明度
callout:{//自定義標記點上方的氣泡窗口 點擊有效
content:'大米和小米兒童康復(廣州盈豐)中心',//文本
color:'#ffffff',//文字顏色
fontSize:14,//文本大小
borderRadius:15,//邊框圓角
borderWidth:'8',
bgColor:'#e51860',//背景顏色
display:'ALWAYS',//常顯
},
},
],
}
},
onLoad() {
},
computed:{
mapheight(){
let data =''
if(this.bottomData){
if(this.upTop){
data ='50px'
}else{
data ='200px'
}
}else{
data ='90vh'
}
return data
},
coverbottom(){
let data =''
if(this.bottomData){
data ='20rpx'
}else{
data ='100rpx'
}
return data
}
},
methods:{
//地圖點擊事件
markertap(e) {
console.log("===你點擊了標記點===",e)
},
//地圖點擊事件
callouttap(e){
console.log('地圖點擊事件',e)
}
}
}
</script>得到的樣式是這樣

這里只給了兩個回調(diào) 有更多需求可以去uniapp中搜索 map | uni-app官網(wǎng)
定位圖標
查了一下官方文檔,發(fā)現(xiàn)官方文檔中并沒有關(guān)于定位的寫法 ,沒辦法只能自己手寫一個了。
大概是這樣的。

<template>
<view class="page-section page-section-gap map-container">
<map style="width: 100%; height: 90vh;" :layer-style='5' :style="{height:mapheight}" :show-location='true' :latitude="latitude" :longitude="longitude" :markers="marker" :scale="scale" @markertap="markertap" @callouttap='callouttap'>
<cover-view class="cover-view" :style='{bottom:coverbottom}' @click="onControltap">
<cover-image class="cover-image" @click="play" src="@/static/ditudingwei.png"></cover-image>
<cover-view>
定位
</cover-view>
</cover-view>
</map>
</view>
</template>
<script>
export default {
data() {
return {
latitude:23.106574, //緯度
longitude:113.324587,//經(jīng)度
scale:13,//縮放級別
bottomData:false,
marker: [
{
id:0,
latitude: 23.13065,//緯度
longitude: 113.3274,//經(jīng)度
iconPath: '', //顯示的圖標
rotate:0, // 旋轉(zhuǎn)度數(shù)
width:20, //寬
height:30, //高
// title:'我在這里',//標注點名
alpha:0.5, //透明度
callout:{//自定義標記點上方的氣泡窗口 點擊有效
content:'天寶大廈',//文本
color:'#ffffff',//文字顏色
fontSize:14,//文本大小
borderRadius:15,//邊框圓角
borderWidth:'10',
bgColor:'#e51860',//背景顏色
display:'ALWAYS',//常顯
},
},
{
id:1234597,
latitude: 23.106574,//緯度
longitude: 113.324587,//經(jīng)度
iconPath: '', //顯示的圖標
rotate:0, // 旋轉(zhuǎn)度數(shù)
width:20, //寬
height:30, //高
// title:'我在這里',//標注點名
alpha:0.5, //透明度
// label:{//為標記點旁邊增加標簽 //因背景顏色H5不支持
// color:'red',//文本顏色
// },
callout:{//自定義標記點上方的氣泡窗口 點擊有效
content:'廣州塔',//文本
color:'#ffffff',//文字顏色
fontSize:14,//文本大小
borderRadius:15,//邊框圓角
borderWidth:'10',
bgColor:'#e51860',//背景顏色
display:'ALWAYS',//常顯
},
},
{
id:2,
latitude: 23.1338,//緯度
longitude: 113.33052,//經(jīng)度
iconPath: '', //顯示的圖標
rotate:0, // 旋轉(zhuǎn)度數(shù)
width:20, //寬
height:30, //高
alpha:0.5, //透明度
callout:{//自定義標記點上方的氣泡窗口 點擊有效
content:'德隆大廈',//文本
color:'#ffffff',//文字顏色
fontSize:14,//文本大小
borderRadius:15,//邊框圓角
borderWidth:'10',
bgColor:'#e51860',//背景顏色
display:'ALWAYS',//常顯
},
},
{
id:3,
latitude: 23.136455,//緯度
longitude: 113.329002,//經(jīng)度
iconPath: '', //顯示的圖標
rotate:0, // 旋轉(zhuǎn)度數(shù)
width:20, //寬
height:30, //高
alpha:0.5, //透明度
callout:{//自定義標記點上方的氣泡窗口 點擊有效
content:'羊城國際商貿(mào)中心',//文本
color:'#ffffff',//文字顏色
fontSize:14,//文本大小
borderRadius:15,//邊框圓角
borderWidth:'10',
bgColor:'#e51860',//背景顏色
display:'ALWAYS',//常顯
},
},
{
id:4,
latitude: 23.224781,//緯度
longitude: 113.293911,//經(jīng)度
iconPath: '', //顯示的圖標
rotate:0, // 旋轉(zhuǎn)度數(shù)
width:20, //寬
height:30, //高
alpha:0.5, //透明度
callout:{//自定義標記點上方的氣泡窗口 點擊有效
content:'天瑞廣場A座',//文本
color:'#ffffff',//文字顏色
fontSize:16,//文本大小
borderRadius:15,//邊框圓角
borderWidth:'12',
bgColor:'#e51860',//背景顏色
display:'ALWAYS',//常顯
},
},
{
id:5,
latitude: 23.072726,//緯度
longitude: 113.277921,//經(jīng)度
iconPath: '', //顯示的圖標
rotate:0, // 旋轉(zhuǎn)度數(shù)
width:20, //寬
height:30, //高
alpha:0.5, //透明度
callout:{//自定義標記點上方的氣泡窗口 點擊有效
content:'大米和小米兒童康復(廣州盈豐)中心',//文本
color:'#ffffff',//文字顏色
fontSize:14,//文本大小
borderRadius:15,//邊框圓角
borderWidth:'8',
bgColor:'#e51860',//背景顏色
display:'ALWAYS',//常顯
},
},
],
}
},
onLoad() {
},
computed:{
mapheight(){
let data =''
if(this.bottomData){
if(this.upTop){
data ='50px'
}else{
data ='200px'
}
}else{
data ='90vh'
}
return data
},
coverbottom(){
let data =''
if(this.bottomData){
data ='20rpx'
}else{
data ='100rpx'
}
return data
}
},
methods:{
//地圖點擊事件
markertap(e) {
console.log("===你點擊了標記點===",e)
},
//地圖點擊事件
callouttap(e){
console.log('地圖點擊事件',e)
}
}
}
</script>
<style lang='less' scoped>
.map-container {
position: relative;
overflow: hidden;
.cover-view {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 80rpx;
height: 80rpx;
color: #484848;
background-color: #fff;
background-size: 120rpx 120rpx;
background-position: center center;
position: absolute;
bottom: 100rpx;
right: 32rpx;
}
.cover-image{
display: inline-block;
width: 30rpx;
height: 30rpx;
}
}
</style>獲取自身經(jīng)緯度
進入頁面拿到用戶自身的經(jīng)緯度
在onShow中輸入
onShow() {
uni.getLocation({
geocode:true,
type: 'wgs84',
success: (res) => {
console.log(res,'獲取當前位置')
},
address:(res) =>{
console.log('address',res)
}
});
需要打開manifest.json

如果從來沒配置過appld請先配置在進行操作


然后重新編譯就會出現(xiàn)

允許之后就可以拿到經(jīng)緯度信息

然后可以將經(jīng)緯度信息賦值給data中的latitude longitude
onShow() {
uni.getLocation({
geocode:true,
type: 'wgs84',
success: (res) => {
console.log(res,'獲取當前位置')
this.longitude =res.longitude
this.latitude = res.latitude
},
address:(res) =>{
console.log('address',res)
}
});
},通過經(jīng)緯度 獲取當前城市信息
首先需要進入騰訊位置服務(wù) /控制臺/應用管理/我的應用/添加key選擇webServiceApi

然后去小程序開發(fā)管理/開發(fā)設(shè)置/服務(wù)器域名/request合法域名后面新增
https://apis.map.qq.com;
然后下載
https://mapapi.qq.com/web/miniprogram/JSSDK/qqmap-wx-jssdk1.1.zip
引入
在ohshow中使用
var qqmapsdk
// 實例化API核心類
qqmapsdk = new QQMapWX({
key: '創(chuàng)建的key'
});
qqmapsdk.reverseGeocoder({
location:{
latitude:this.latitude,
longitude:this.longitude
},
success:(res)=>{
console.log('reverseGeocoder',res)
},
fail:(err)=>{
console.log('reverseGeocoder',err)
}
})這樣就可以通過自身的經(jīng)緯度獲取到當前所在的詳細信息
更多地圖事件可以查詢官網(wǎng)騰訊位置服務(wù) - 立足生態(tài),連接未來
關(guān)于地圖氣泡彈窗在真機不顯示或者顯示顏色不明顯的情況,你可以使用在callout中使用padding
這樣就可以顯示顏色了
在uniapp中使用地圖大概就這些
總結(jié)
到此這篇關(guān)于uniapp地圖組件(map)使用與遇到的一些問題總結(jié)的文章就介紹到這了,更多相關(guān)uniapp地圖組件(map)使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS實現(xiàn)自動定時切換的簡潔網(wǎng)頁選項卡效果
這篇文章主要介紹了JS實現(xiàn)自動定時切換的簡潔網(wǎng)頁選項卡效果,涉及JavaScript基于時間函數(shù)定時觸發(fā)遍歷函數(shù)實現(xiàn)定時切換功能,需要的朋友可以參考下2015-10-10
Bootstrap實現(xiàn)基于carousel.js框架的輪播圖效果
這篇文章主要為大家詳細介紹了Bootstrap實現(xiàn)基于carousel.js框架的輪播圖效果,無過渡動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
關(guān)于Mozilla瀏覽器不支持innerText的解決辦法

