百度智能小程序 地圖

2020-09-04 13:48 更新

map 地圖

解釋:地圖,客戶端創(chuàng)建的 原生組件,使用時(shí)請(qǐng)注意相關(guān)限制。

屬性說明

屬性名 類型 默認(rèn)值 必填 說明 最低版本 Web 態(tài)說明
longitude Number gcj02 坐標(biāo)系中心經(jīng)度 - -
latitude Number gcj02 坐標(biāo)系中心緯度 - -
scale Number 16 縮放級(jí)別,取值范圍為 4-21 。 - -
markers Array.<marker> 標(biāo)記點(diǎn) - -
polyline Array.<polyline> 路線 - -
polygons Array.<polygon> 多邊形(工具暫不支持) 2.0.13
低版本請(qǐng)做兼容性處理
-
circles Array.<circle> - -
controls Array.<control> 控件 - -
include-points Array.<point> 縮放視野以包含所有給定的坐標(biāo)點(diǎn) - -
show-location Boolean false 顯示帶有方向的當(dāng)前定位點(diǎn) - 暫時(shí)不能顯示方向
enable-3D Boolean false 顯示 3D 樓塊(工具暫不支持) 2.0.13
低版本請(qǐng)做兼容性處理
暫不支持
show-compass Boolean false 顯示指南針(工具暫不支持) 2.0.13
低版本請(qǐng)做兼容性處理
暫不支持
enable-overlooking Boolean false 開啟俯視(工具暫不支持) 2.0.13
低版本請(qǐng)做兼容性處理
暫不支持
enable-zoom Boolean true 是否支持縮放(工具暫不支持) 2.0.13
低版本請(qǐng)做兼容性處理
-
enable-scroll Boolean true 是否支持拖動(dòng)(工具暫不支持) 2.0.13
低版本請(qǐng)做兼容性處理
-
enable-rotate Boolean false 是否支持旋轉(zhuǎn)(工具暫不支持) 2.0.13
低版本請(qǐng)做兼容性處理
暫不支持
bindmarkertap EventHandle 點(diǎn)擊標(biāo)記點(diǎn)時(shí)觸發(fā) - -
bindcallouttap EventHandle 點(diǎn)擊標(biāo)記點(diǎn)對(duì)應(yīng)的氣泡時(shí)觸發(fā) - -
bindcontroltap EventHandle 點(diǎn)擊控件時(shí)觸發(fā) - -
bindregionchange EventHandle 視野發(fā)生變化時(shí)觸發(fā) - -
bindtap EventHandle 點(diǎn)擊地圖時(shí)觸發(fā) - -
bindupdated EventHandle 在地圖渲染更新完成時(shí)觸發(fā) - -
bindpoitap EventHandle 點(diǎn)擊地圖 poi 點(diǎn)時(shí)觸發(fā) -暫不支持

示例 

在開發(fā)者工具中打開


代碼示例

<view class="wrap">
    <view class="card-area">
        <map class="map"
            longitude="{{longitude}}"
            latitude="{{latitude}}"
            scale="{{scale}}"
            markers="{{markers}}"
            polyline="{{polyline}}"
            polygons="{{drawPolygon ? polygons : []}}"
            circles="{{circles}}"
            controls="{{controls}}"
            show-location="{{showLocation}}"
            include-points="{{includePoints}}"
            enable-3D="{{enable3d}}"
            show-compass="{{showCompass}}"
            enable-overlooking="{{enableOverlooking}}"
            enable-zoom="{{enableZoom}}"
            enable-scroll="{{enableScroll}}"
            enable-rotate="{{enableRotate}}"
            bindmarkertap="onMarkertap" 
            bindcallouttap="onCallouttap" 
            bindcontroltap="onControltap" 
            bindregionchange="onRegionchange" 
            bindtap="onTap" 
            bindupdated="onUpdated"
            bindpoitap="onPoitap">
        </map>
        <view class="item-scroll block border-bottom">
            <text class="switch-text">支持縮放</text>
            <switch bindchange="toggleZoom" checked></switch>
        </view>
        <view class="item-scroll block border-bottom">
            <text class="switch-text">支持拖動(dòng)</text>
            <switch bindchange="toggleScroll" checked></switch>
        </view>
        <view class="item-scroll block border-bottom" s-if="{{!isWeb}}">
            <text class="switch-text">支持旋轉(zhuǎn)</text>
            <switch bindchange="toggleRotate" checked></switch>
        </view>
    </view>
    <view class="tip-week">工具暫不支持手勢控制</view>
</view>
Page({
    data: {
        latitude: '40.048828',
        longitude: '116.280412',
        scale: 16,
        polygons: [],
        drawPolygon: false,
        includePoints: [],
        showLocation: true,
        enable3d: false,
        showCompass: false,
        enableOverlooking: false,
        enableZoom: true,
        enableScroll: true,
        enableRotate: true,
        markers: [{
            markerId: '1',
            latitude: '40.052751',
            longitude: '116.278796'
        }, {
            markerId: '2',
            latitude: '40.048828',
            longitude: '116.280412',
            callout: {
                display: 'ALWAYS',
                content: '百度科技園'
            }
        }, {
            markerId: '3',
            latitude: '40.049655',
            longitude: '116.27505',
            callout: {
                display: 'ALWAYS',
                content: '西山壹號(hào)院'
            }
        }],
        polyline: [{
            points: [{
                longitude: 116.278796,
                latitude: 40.048828
            }, {
                longitude: 116.27505,
                latitude: 40.049655
            }],
            color: '#FF5F41FF',
            width: 2,
            dottedLine: true
        }],
        controls: [{
            controlId: 1,
            iconPath: 'https://b.bdstatic.com/searchbox/icms/searchbox/img/api_logo.png',
            position: {
                left: 0,
                top: 100,
                width: 50,
                height: 50
            },
            clickable: true
        }],
        circles: [{
            latitude: '40.052751',
            longitude: '116.278796',
            color: '#FF5F41FF',
            fillColor: '#21FFFFFF',
            radius: '200',
            strokeWidth: '2'
        }]
    },
    showLocation() {
        this.setData({
            showLocation: !this.data.showLocation
        });
    },
    toggle3d() {
        this.setData({
            enable3d: !this.data.enable3d
        });
    },
    toggleShowCompass() {
        this.setData({
            showCompass: !this.data.showCompass
        });
    },
    toggleOverlooking() {
        this.setData({
            enableOverlooking: !this.data.enableOverlooking
        });
    },
    toggleZoom() {
        this.setData({
            enableZoom: !this.data.enableZoom
        });
    },
    toggleScroll() {
        this.setData({
            enableScroll: !this.data.enableScroll
        });
    },
    toggleRotate() {
        this.setData({
            enableRotate: !this.data.enableRotate
        });
    },
    togglePolygon() {
        this.setData({
            drawPolygon: !this.data.drawPolygon
        });
    },
    onMarkertap(e) {
        console.log('onMarkertap callback:', e);
    },
    onCallouttap(e) {
        console.log('onCallouttap callback:', e);
    },
    onControltap(e) {
        console.log('onControltap callback:', e);
    },
    onRegionchange(e) {
        console.log('onRegionchange callback:', e);
    },
    onTap(e) {
        console.log('onTap callback:', e);
    },
    onUpdated(e) {
        console.log('onUpdated callback:', e);
    },
    onPoitap(e) {
        console.log('onPoitap callback:', e);
    }
});

markers

解釋:標(biāo)記點(diǎn),用于在地圖上顯示標(biāo)記的位置。

屬性說明

屬性名說明類型必填備注
markerId標(biāo)記點(diǎn) idNumbermarker 點(diǎn)擊事件回調(diào)會(huì)返回此 id。建議為每個(gè) marker 設(shè)置 Number 類型的 id,保證更新 marker 時(shí)有更好的性能。
latitude緯度Number浮點(diǎn)數(shù),范圍 -90 ~ 90 。
longitude經(jīng)度Number浮點(diǎn)數(shù),范圍 -180 ~ 180 。
zIndex顯示層級(jí)Number
iconPath顯示的圖標(biāo)String項(xiàng)目目錄下的圖片路徑,支持網(wǎng)絡(luò)路徑、本地路徑(相對(duì)和絕對(duì)路徑寫法),工具暫不支持絕對(duì)路徑寫法。
rotate旋轉(zhuǎn)角度Number默認(rèn)為 0,順時(shí)針旋轉(zhuǎn)的角度,范圍 0 ~ 360 。
alpha標(biāo)注的透明度Number默認(rèn)為 1,無透明,范圍 0 ~ 1 。
width標(biāo)注圖標(biāo)寬度Number默認(rèn)為圖片實(shí)際寬度
height標(biāo)注圖標(biāo)高度Number默認(rèn)為圖片實(shí)際高度
callout自定義標(biāo)記點(diǎn)上方的氣泡窗口Object支持的屬性見下表,可識(shí)別換行符。
label為標(biāo)記點(diǎn)旁邊增加標(biāo)簽Object支持的屬性見下表,可識(shí)別換行符。
anchor經(jīng)緯度在標(biāo)注圖標(biāo)的錨點(diǎn)Object默認(rèn)底邊中點(diǎn),{x, y},x 表示橫向(0-1),y 表示豎向(0-1)。{x: .5, y: 1} 表示底邊中點(diǎn)。

callout 屬性說明

屬性名說明類型備注
content文本String
color文本顏色String8 位十六進(jìn)制表示,最后 2 位表示 alpha 值(可省略)。
fontSize文字大小Number
borderWidth邊框?qū)挾?/td>Number
borderColor邊框顏色String8 位十六進(jìn)制表示,最后 2 位表示 alpha 值(可省略)。
borderRadiuscallout 邊框圓角Number
bgColor背景色String8 位十六進(jìn)制表示,最后 2 位表示 alpha 值(可省略)。
padding文本邊緣留白Number
display‘BYCLICK’:點(diǎn)擊顯示; ‘ALWAYS’:常顯String默認(rèn)為常顯
textAlign文本對(duì)齊方式。有效值: left , right , center 。String默認(rèn)為居中對(duì)齊

label 屬性說明

屬性名說明類型備注
content文本String
color文本顏色String8 位十六進(jìn)制表示,最后 2 位表示 alpha 值(可省略)。
fontSize文字大小Number
xlabel 的坐標(biāo),原點(diǎn)是 marker 對(duì)應(yīng)的經(jīng)緯度Number
ylabel 的坐標(biāo),原點(diǎn)是 marker 對(duì)應(yīng)的經(jīng)緯度Number
borderWidth邊框?qū)挾?/td>Number
borderColor邊框顏色String8 位十六進(jìn)制表示,最后 2 位表示 alpha 值(可省略)。
borderRadius邊框圓角Number
bgColor背景色String8 位十六進(jìn)制表示,最后 2 位表示 alpha 值(可省略)。
padding文本邊緣留白Number
textAlign文本對(duì)齊方式。有效值: left , right , center 。String默認(rèn)為居中對(duì)齊

參考示例

圖片示例


代碼示例: 

在開發(fā)者工具中打開

<view class="wrap">
    <map
        style="width: 100%; height: 300px;"
        longitude="{{longitude}}"
        latitude="{{latitude}}"
        markers="{{markers}}"
        bindmarkertap="bindmarkertap"
        bindcallouttap="bindcallouttap">
    </map>
</view>
Page({
    data: {
        latitude: 40.048828,
        longitude: 116.280412,
        markers: [{
            markerId: 1,
            latitude: 40.048828,
            longitude: 116.280412,
            title: 'markerId: 1',
            zIndex: 100,
            iconPath: '../images/location.png',
            rotate: 90,
            callout: {
                display: 'ALWAYS',
                content: '百度科技園',
                color: '#000',
                fontSize: '16',
                borderRadius: 50,
                bgColor: '#5B9FFF',
                padding: 2,
                textAlign: 'center'
            }
        }, {
            markerId: 2,
            latitude: 40.049655,
            longitude: 116.27505,
            title: 'markerId: 2',
            zIndex: 100,
            rotate: 90,
            alpha: 0.5,
            width: 30,
            height: 50,
            label: {
                'content': 'label',
                'color': '#7B68EE',
                'fontSize': 16,
                'borderWidth': 1,
                'borderColor': '#5B9FFF',
                'borderRadius': 50,
                'bgColor': '#ADCFFF',
                'padding': 5,
                'textAlign': 'center'
            },
            anchor: {x: .5, y: 1},
            callout: {
                display: 'BYCLICK',
                content: '西山壹號(hào)院',
                color: '#FFF',
                fontSize: '16',
                borderRadius: 50,
                bgColor: '#5B9FFF',
                padding: 2,
                textAlign: 'center'
            }
        }]
    },
    bindmarkertap() {
        swan.showToast({
            title: '點(diǎn)擊標(biāo)記啦',
            icon: 'none'
        });
    },
    bindcallouttap() {
        swan.showToast({
            title: '點(diǎn)擊標(biāo)記上方氣泡啦',
            icon: 'none'
        });
    }
});

polyline

解釋:指定一系列坐標(biāo)點(diǎn),從數(shù)組第一項(xiàng)連線至最后一項(xiàng)。

屬性說明

屬性名說明類型必填備注
points經(jīng)緯度數(shù)組Array[{latitude: 0, longitude: 0}]
color線的顏色String8 位十六進(jìn)制表示,最后 2 位表示 alpha 值(可省略)。
width線的寬度Number
dottedLine是否虛線Boolean默認(rèn) false
arrowLine帶箭頭的線Boolean默認(rèn) false
arrowIconPath更換箭頭圖標(biāo)String在 arrowLine 為 true 時(shí)生效
borderColor線的邊框顏色String8 位十六進(jìn)制表示,最后 2 位表示 alpha 值(可省略)。
borderWidth線的厚度Number

示例

圖片示例

代碼示例 

在開發(fā)者工具中打開

<view class="wrap">
    <map
        style="width: 100%; height: 300px;"
        longitude="{{longitude}}"
        latitude="{{latitude}}"
        polyline="{{polyline}}">
    </map>
</view>

polygon

解釋:指定一系列坐標(biāo)點(diǎn),根據(jù) points 坐標(biāo)數(shù)據(jù)生成閉合多邊形。

屬性說明

屬性名說明類型必填備注
points經(jīng)緯度數(shù)組Array[{latitude: 0, longitude: 0}]
strokeWidth描邊的寬度Number
strokeColor描邊的顏色String8 位十六進(jìn)制表示,最后 2 位表示 alpha 值(可省略)。
fillColor填充顏色String8 位十六進(jìn)制表示,最后 2 位表示 alpha 值(可省略)。
zIndex設(shè)置多邊形 Z 軸數(shù)值Number

示例

圖片示例


代碼示例

在開發(fā)者工具中打開

<view class="wrap">
    <map
        style="width: 100%; height: 300px;"
        longitude="{{longitude}}"
        latitude="{{latitude}}"
        polygons="{{polygons}}">
    </map>
</view>
Page({
    data: {
        latitude: 40.048828,
        longitude: 116.280412,
        polygons: [{
            points: [{
                longitude: 116.278796,
                latitude: 40.048828
            }, {
                longitude: 116.27505,
                latitude: 40.049655
            },{
                longitude: 116.27305,
                latitude: 40.050655
            },{
                longitude: 116.27105,
                latitude: 40.051655
            }],
            strokeWidth: 1,
            strokeColor: '#000000AA',
            fillColor: '#000000AA',
            zIndex: 20
        }]
    }
});

circle

解釋:在地圖上顯示圓

屬性說明

屬性名說明類型必填備注
latitude緯度Number浮點(diǎn)數(shù),范圍 -90 ~ 90 。
longitude經(jīng)度Number浮點(diǎn)數(shù),范圍 -180 ~ 180 。
color描邊的顏色String8 位十六進(jìn)制表示,最后 2 位表示 alpha 值(可省略)。
fillColor填充顏色String8 位十六進(jìn)制表示,最后 2 位表示 alpha 值(可省略)。
radius半徑Number
strokeWidth描邊的寬度Number

示例

圖片示例


代碼示例 

在開發(fā)者工具中打開

<view class="wrap">
    <map
        style="width: 100%; height: 300px;"
        longitude="{{longitude}}"
        latitude="{{latitude}}"
        circles="{{circles}}">
    </map>
</view>
Page({
    data: {
        scale: 16,
        latitude: 40.048828,
        longitude: 116.280412,
        circles: [{
            latitude: 40.052751,
            longitude: 116.278796,
            color: '#FF5F41FF',
            fillColor: '#FF5F41FF',
            radius: 200,
            strokeWidth: 2
        }]
    }
});

control

解釋:在地圖上顯示控件,控件不隨著地圖移動(dòng)。

屬性說明

屬性名說明類型必填備注
controlId控件 idNumber在控件點(diǎn)擊事件回調(diào)會(huì)返回此 id
position控件在地圖的位置Object控件相對(duì)地圖位置
iconPath顯示的圖標(biāo)String項(xiàng)目目錄下的圖片路徑,支持網(wǎng)絡(luò)路徑、本地路徑(相對(duì)和絕對(duì)路徑寫法),工具暫不支持絕對(duì)路徑寫法。
clickable是否可點(diǎn)擊Boolean默認(rèn)不可點(diǎn)擊

position 屬性說明

屬性名說明類型必填備注
left距離地圖的左邊界多遠(yuǎn)Number默認(rèn)為 0
top距離地圖的上邊界多遠(yuǎn)Number默認(rèn)為 0
width控件寬度Number默認(rèn)為圖片寬度
height控件高度Number默認(rèn)為圖片高度

示例

圖片示例


代碼示例 

在開發(fā)者工具中打開

<view class="wrap">
    <map
        style="width: 100%; height: 300px;"
        longitude="{{longitude}}"
        latitude="{{latitude}}"
        controls="{{controls}}"
        bindcontroltap="bindcontroltap"
        >
    </map>
</view>
Page({
    data: {
        latitude: 40.048828,
        longitude: 116.280412,
        controls: [{
            controlId: 1,
            iconPath: '/images/navigate.png',
            position: {
                left: 0,
                top: 100,
                width: 50,
                height: 50
            },
            clickable: true
        }]
    },
    bindcontroltap() {
        swan.showToast({
            title: '點(diǎn)擊控件',
            icon: 'none'
        })
    }
});

代碼示例 - 錯(cuò)誤用法:原生組件設(shè)置 border 無效,也不可用 cover-view 覆蓋為圓角

在開發(fā)者工具中打開

<view class="wrap">
    <cover-view class="card-area">
        <map
            longitude="{{longitude}}"
            latitude="{{latitude}}">
        </map>
    </cover-view>
</view>
.card-area { 
    width: 3.88rem;
    height: 2.18rem;
}

Bug & Tip

  • Tip:地圖組件的經(jīng)緯度必填,如果不填經(jīng)緯度則默認(rèn)值是北京的經(jīng)緯度。
  • Tip:map 組件是由客戶端創(chuàng)建的原生組件,它的層級(jí)是最高的,不能通過 z-index 控制層級(jí)。
  • Tip:請(qǐng)勿在 scroll-view、swiper、picker-view、movable-view 中使用 map 組件。
  • Tip:CSS 動(dòng)畫對(duì) map 組件無效。
  • Tip:cover-view、cover-image 組件可覆蓋在 map 組件之上。
  • Bug:map 組件的 markers 的 label 暫不支持換行。
  • Tip:Android 與 iOS 定位精度不同,雙端定位存在差異。
  • Tip:map 組件使用的經(jīng)緯度是火星坐標(biāo)系,調(diào)用 swan.getLocation 接口需要指定 type 為 gcj02 。
  • Tip:開發(fā)者工具由于坐標(biāo)系不同,定位與客戶端存在差異。開發(fā)時(shí)請(qǐng)以客戶端為準(zhǔn)。
  • Bug:Web 態(tài)由于坐標(biāo)系不同,定位與客戶端存在差異,坐標(biāo)與客戶端相比有一定偏移。待后續(xù)版本修復(fù)。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)