前言
在地圖應(yīng)用中空骚,Overlay
(覆蓋層)是指在地圖圖層之上疊加顯示的額外信息層。這些覆蓋層可以用來展示各種數(shù)據(jù)未檩,如交通狀況、用戶位置粟焊、興趣點(diǎn)(POI)等冤狡。在騰訊地圖SDK中,可以使用多種類型的覆蓋層來增強(qiáng)地圖的功能和視覺效果项棠。文本介紹簡單的點(diǎn)線面覆蓋物使用方法和屬性悲雳。
地圖類中覆蓋物的接口
-
TencentMap
類中添加Overlay
的接口:
類型 |
方法 |
說明 |
Marker |
addMarker (MarkerOptions options) |
添加標(biāo)注(Marker) |
Polyline |
addPolyline (PolylineOptions options) |
添加線 |
Polygon |
addPolygon (PolygonOptions options) |
添加多邊形 |
Arc |
addArc (ArcOptions options) |
添加弧線 |
Circle |
addCircle (CircleOptions options) |
添加圓 |
-
TencentMap
類中刪除Overlay
的接口:
類型 |
方法 |
說明 |
void |
clearAllOverlays () |
清除地圖上所有的標(biāo)注類(Marker、Polyline香追、Polygon合瓢,TileOverlay除外) |
類型 |
方法 |
說明 |
void |
remove (); |
從地圖移除覆蓋物 |
boolean |
isRemoved (); |
|
void |
releaseData (); |
|
覆蓋物類
類型 |
覆蓋物類 |
選項(xiàng) |
點(diǎn) |
Marker |
MarkerOptions |
折線 |
Polyline |
PolylineOptions |
多邊形 |
Polygon |
PolygonOptions |
弧線 |
Arc |
ArcOptions |
圓 |
Circle |
CircleOptions |
Marker示例
// 定義Maker坐標(biāo)點(diǎn)
LatLng point = new LatLng(39.963175, 116.400244);
// 構(gòu)建Marker圖標(biāo)
BitmapDescriptor bitmap = BitmapDescriptorFactory
.fromResource(R.drawable.icon_mark_a);
// 構(gòu)建MarkerOption,用于在地圖上添加Marker
MarkerOptions option = new MarkerOptions(point)
.position(point)
.icon(bitmap);
// 在地圖上添加Marker透典,并顯示
Marker marker = map.addMarker(option);
Polyline示例
// 構(gòu)建折線點(diǎn)坐標(biāo)
LatLng p1 = new LatLng(39.97923, 116.357428);
LatLng p2 = new LatLng(39.94923, 116.397428);
LatLng p3 = new LatLng(39.97923, 116.437428);
List<LatLng> points = new ArrayList<>();
points.add(p1);
points.add(p2);
points.add(p3);
// 設(shè)置折線的屬性
PolylineOptions polylineOptions = new PolylineOptions()
.width(10)
.color(0xAA00FF00)
.addAll(points);
// 在地圖上繪制折線
Polyline polyline = map.addPolyline(polylineOptions);
Polygon示例
// 多邊形頂點(diǎn)位置
List<LatLng> points = new ArrayList<>();
points.add(new LatLng(39.93923, 116.357428));
points.add(new LatLng(39.91923, 116.327428));
points.add(new LatLng(39.89923, 116.347428));
points.add(new LatLng(39.89923, 116.367428));
points.add(new LatLng(39.91923, 116.387428));
// 構(gòu)造PolygonOptions
PolygonOptions polygonOptions = new PolygonOptions()
.addAll(points)
.fillColor(0xAAFFFF00) // 填充顏色
.strokeColor(0xAA00FF00) // 邊框顏色
.strokeWidth(5); // 邊框?qū)挾?
// 在地圖上顯示多邊形
Polygon polygon = map.addPolygon(polygonOptions);
Arc示例
// 添加弧線坐標(biāo)數(shù)據(jù)
LatLng p1 = new LatLng(39.97923, 116.357428); // 起點(diǎn)
LatLng p2 = new LatLng(40.00923, 116.397428); // 中間點(diǎn)
LatLng p3 = new LatLng(39.97923, 116.437428); // 終點(diǎn)
// 構(gòu)造ArcOptions對(duì)象
ArcOptions arcOptions = new ArcOptions()
.color(Color.RED)
.width(10)
.points(p1, p3)
.pass(p2);
// 在地圖上顯示弧線
Arc arc = map.addArc(arcOptions);
Circle示例
// 圓心位置
LatLng center = new LatLng(39.90923, 116.447428);
// 構(gòu)造CircleOptions對(duì)象
CircleOptions circleOptions = new CircleOptions()
.center(center)
.radius(1400)
.fillColor(0xAAFFFF00) // 填充顏色
.strokeColor(0xAA00ff00) // 邊框顏色
.strokeWidth(5); // 邊框?qū)挾?
// 在地圖上顯示圓
Circle circle = map.addCircle(circleOptions);
移除示例
// 清除地圖上所有的標(biāo)注類(Marker晴楔、Polyline、Polygon峭咒,TileOverlay除外)
map.clearAllOverlays();
marker.remove();
polyline.remove();
polygon.remove();
arc.remove();
circle.remove();
效果圖
Marker的更多屬性
點(diǎn)標(biāo)記滥崩,是在地圖上用來標(biāo)記一個(gè)經(jīng)緯度坐標(biāo)的覆蓋物,包括點(diǎn)圖標(biāo)和浮在點(diǎn)之上的信息窗(常稱之為InfoWindow)讹语。騰訊地圖SDK為點(diǎn)標(biāo)記提供了豐富的樣式和場景使用钙皮。
騰訊地圖SDK提供很多默認(rèn)的Marker樣式,通過TencentMap添加Marker接口顽决,即可添一個(gè)默認(rèn)樣式的Marker短条。
Marker marker = mTencentMap.addMarker(new MarkerOptions(position));
開發(fā)者如果想自定義Marker的樣式屬性,可以通過兩個(gè)階段進(jìn)行修改:
- 創(chuàng)建Marker對(duì)象之前才菠,可以通過修改MarkerOptions選項(xiàng)來初始化Marker
- 創(chuàng)建Marker對(duì)象之后茸时,可以通過Marker對(duì)象,來修改屬性
常用屬性
類型 |
方法 |
說明 |
MarkerOptions |
icon (BitmapDescriptor icon) |
設(shè)置標(biāo)注的樣式 |
MarkerOptions |
position (LatLng latlng) |
設(shè)置標(biāo)注的位置 |
MarkerOptions |
alpha (float alpha) |
設(shè)置標(biāo)注的透明度 |
MarkerOptions |
flat (boolean flat) |
設(shè)置是不是3D標(biāo)注赋访,3D標(biāo)注會(huì)隨著地圖傾斜面傾斜 |
MarkerOptions |
anchor (float anchorU, float anchorV) |
設(shè)置標(biāo)注的錨點(diǎn) |
MarkerOptions |
clockwise (boolean clockwise) |
旋轉(zhuǎn)角度是否沿順時(shí)針方向 |
MarkerOptions |
rotation (float rotation) |
設(shè)置標(biāo)注的旋轉(zhuǎn)角度 |
MarkerOptions |
visible (boolean flag) |
設(shè)置標(biāo)注是否可見 |
交互
- 啟用可點(diǎn)擊時(shí)可都,在點(diǎn)擊
Marker
時(shí)缓待,會(huì)回調(diào)TencentMap.OnMarkerClickListener
。
- 啟用允許拖拽時(shí)渠牲,在拖拽
Marker
時(shí)旋炒,會(huì)回調(diào)TencentMap.OnMarkerDragListener
。
類型 |
方法 |
說明 |
MarkerOptions |
draggable (boolean flag) |
設(shè)置標(biāo)注是否可以被拖動(dòng) |
碰撞
騰訊地圖SDK4.4.1版本起签杈,支持點(diǎn)標(biāo)記與地圖POI元素的碰撞功能瘫镇,當(dāng)開啟碰撞功能時(shí),與點(diǎn)標(biāo)記重合的POI元素將會(huì)被隱藏答姥。
類型 |
方法 |
說明 |
MarkerOptions |
collisionBy (MarkerCollisionItem... item) |
設(shè)置可被碰撞的類型铣除,默認(rèn)無碰撞關(guān)系 構(gòu)造子MarkerMarkerCollisionRelationUnit時(shí),如果主Marker的getCollisionRelation() == MarkerCollisionRelation.TOGETHER,碰撞類型不支持配置鹦付,默認(rèn)使用主Marker的碰撞類型 |
MarkerOptions |
setCollisionRelation (MarkerCollisionRelation collisionRelation) |
設(shè)置碰撞關(guān)聯(lián)類型 |
動(dòng)畫
- Marke支持輪播的功能尚粘,通過
Marker
類setIconLooper
方法來傳入輪播圖數(shù)組,并通過MarkerOptions
類iconLooper
設(shè)定輪播模式敲长。
-
Marker
還支持Animation
動(dòng)畫郎嫁,目前支持平移、縮放潘明、旋轉(zhuǎn)行剂、漸變
四種秕噪。通過Marker類setAnimation方法設(shè)置钳降。
類型 |
方法 |
說明 |
MarkerOptions |
iconLooper (boolean enable) |
設(shè)置圖標(biāo)輪播模式, 默認(rèn)周期時(shí)長為300ms |
MarkerOptions |
iconLooper (boolean enable, int duration) |
設(shè)置圖標(biāo)輪播模式 |
其它屬性
類型 |
方法 |
說明 |
MarkerOptions |
title (String s) |
設(shè)置標(biāo)注的InfoWindow(氣泡)的標(biāo)題,如果設(shè)置了 TencentMap.setInfoWindowAdapter(TencentMap.InfoWindowAdapter) 則失效 |
MarkerOptions |
snippet (String snippet) |
設(shè)置標(biāo)注的InfoWindow(氣泡)的內(nèi)容腌巾,如果設(shè)置了 TencentMap.setInfoWindowAdapter(TencentMap.InfoWindowAdapter) 則失效 |
MarkerOptions |
infoWindowEnable (boolean enabled) |
設(shè)置標(biāo)注是否可以彈出InfoWindow(氣泡) |
MarkerOptions |
viewInfoWindow (boolean enabled) |
設(shè)置此 marker 的 infowindow 是否用 view 實(shí)現(xiàn) infowindow |
MarkerOptions |
infoWindowAnchor (float u, float v) |
設(shè)置infowindow anchor point |
MarkerOptions |
infoWindowOffset (int offsetX, int offsetY) |
設(shè)置InfoWindow的偏移遂填,在基準(zhǔn)(InfoWindow在Marker正上方中間處顯示 —— 默認(rèn)位置)上偏移 方向:向右,向下為正方向澈蝙,向左,向上為負(fù)方向 |
MarkerOptions |
infoWindowCollisionBy (MarkerCollisionItem... infoWindowCollisions) |
設(shè)置InfoWindow 可被碰撞的類型吓坚,默認(rèn)無碰撞關(guān)系 |
MarkerOptions |
zIndex (float zIndex) |
設(shè)置相同顯示Level level(int) 的Marker的堆疊順序,相同顯示level灯荧,zIndex越大越靠上顯示 level優(yōu)先級(jí)大于zIndex |
MarkerOptions |
level (int level) |
設(shè)置Marker的顯示Level礁击,level用于控制Marker與樓塊,道路逗载,POI的顯示層級(jí)關(guān)系哆窿。 |
MarkerOptions |
tag (Object tag) |
設(shè)置標(biāo)簽對(duì)象 |
MarkerOptions |
indoorInfo (IndoorInfo indoorInfo) |
為Marker添加室內(nèi)圖信息(buildingId,floorName) 當(dāng)添加室內(nèi)圖信息后厉斟,Marker將會(huì)隨著綁定的室內(nèi)圖狀態(tài)改變而展示和隱藏 |
MarkerOptions |
fastLoad (boolean fastLoad) |
設(shè)置是否允許 marker 快速加載挚躯,默認(rèn)此選項(xiàng)為 true 如果當(dāng)前屏幕內(nèi)有大量 marker 動(dòng)態(tài)變化其 icon 可能會(huì)有性能問題 可以嘗試關(guān)閉不需要此功能的 marker,關(guān)閉此功能后可能導(dǎo)致切換 icon 時(shí)閃爍 |
MarkerOptions |
contentDescription (String contentDescription) |
設(shè)置無障礙相關(guān)描述信息 |
折線的更多屬性
線是由一組經(jīng)緯度點(diǎn)按照一定的順序連接而成擦秽,在地圖上繪制線由 Polyline 類定義實(shí)現(xiàn)码荔。通常用來表示一段路漩勤、軌跡等線型場景。
常用屬性
類型 |
方法 |
說明 |
PolylineOptions |
width (float width) |
設(shè)置線寬度 |
PolylineOptions |
color (int i) |
設(shè)置線的顏色 |
PolylineOptions |
alpha (float a) |
設(shè)置透明度 |
PolylineOptions |
add (LatLng[] latLngs) |
添加頂點(diǎn) |
PolylineOptions |
add (LatLng latlng, LatLng... args) |
添加頂點(diǎn) |
PolylineOptions |
addAll (Iterable< LatLng > latLngs) |
添加頂點(diǎn) |
PolylineOptions |
latLngs (List< LatLng > listLatlngs) |
設(shè)置折線坐標(biāo) |
PolylineOptions |
updatePoints (Iterable< LatLng > latLngs) |
添加頂點(diǎn),該方法會(huì)清空原來頂點(diǎn)集合 |
PolylineOptions |
colors (int[] colors, int[] indexes) |
設(shè)置線的分段顏色 |
PolylineOptions |
colorTexture (BitmapDescriptor bitmapDescriptor) |
注意:1缩搅、當(dāng)調(diào)用此接口越败,且LineType不為LINE_TYPE_DOTTEDLINE時(shí),color和colors接口指定的值代表用此接口設(shè)置紋理的第幾像素行誉己,以繪制紋理線眉尸。2、當(dāng)調(diào)用此接口巨双,且LineType設(shè)置為LINE_TYPE_DOTTEDLINE時(shí)噪猾,繪制線時(shí)會(huì)連續(xù)繪制此接口設(shè)置的紋理。 |
PolylineOptions |
colorType (PolylineOptions.ColorType type) |
設(shè)置顏色類型 |
PolylineOptions |
visible (boolean visible) |
設(shè)置折線可見性 |
PolylineOptions |
lineType (int lineType) |
設(shè)置線的類型筑累,必須LineType里面的一種 |
PolylineOptions |
pattern (List< Integer > pattern) |
設(shè)置ARGB虛線的樣式 |
交互
類型 |
方法 |
說明 |
PolylineOptions |
clickable (boolean clickable) |
是否可以點(diǎn)擊 |
其它屬性
類型 |
方法 |
說明 |
PolylineOptions |
gradient (boolean enable) |
漸變色開關(guān) 只有 getLineType() == PolylineOptions.LineType.LINE_TYPE_MULTICOLORLINE 且 isRoad() == true 才生效 |
PolylineOptions |
borderColor (int borderColor) |
設(shè)置線ARGB的描邊顏色:當(dāng)線是純色線的時(shí)候袱蜡,設(shè)置border的顏色可用此接口 |
PolylineOptions |
borderColors (int[] borderColors) |
設(shè)置ARGB線的描邊顏色,borderColors的數(shù)量應(yīng)該與 colors(int[], int[])接口中的colors的長度保持一致慢宗。 |
PolylineOptions |
borderWidth (float borderWidth) |
設(shè)置ARGB線 描邊的寬度 |
PolylineOptions |
eraseColor (int eraseColor) |
設(shè)置ARGB類型線的擦除色坪蚁,默認(rèn)為Color.GRAY |
PolylineOptions |
lineCap (boolean cap) |
設(shè)置路線是否顯示半圓端點(diǎn) |
PolylineOptions |
zIndex (int zIndex) |
設(shè)置相同顯示Level level(int) 的折線堆疊順序,相同顯示level镜沽,zIndex越大越靠上顯示 level優(yōu)先級(jí)大于zIndex |
PolylineOptions |
animation (Animation a) |
執(zhí)行一個(gè)動(dòng)畫 |
PolylineOptions |
indoorInfo (IndoorInfo indoorInfo) |
為Polyline添加室內(nèi)圖信息(buildingId敏晤,floorName) 當(dāng)添加室內(nèi)圖信息后,Marker將會(huì)隨著綁定的室內(nèi)圖狀態(tài)改變而展示和隱藏 |
PolylineOptions |
level (int level) |
設(shè)置Polyline的顯示Level缅茉,level用于控制Polyline與樓塊嘴脾,道路,POI的顯示層級(jí)關(guān)系蔬墩。 |
PolylineOptions |
road (boolean isRoad) |
設(shè)置線是否為路線 |
PolylineOptions |
text (PolylineOptions.Text text) |
設(shè)置沿 polyline 展示的文字译打。只要調(diào)用 TencentMap.addPolyline(PolylineOptions) 構(gòu)造出 PolylineOptions.Text 就不可以再調(diào)用 Polyline.setPoints(List) 修改展示的路徑,只能創(chuàng)建新的 Polyline |
多邊形的更多屬性
多邊形是由 Polygon
類定義的一組在地圖上的封閉線段組成的圖形拇颅,它由一組 LatLng 點(diǎn)按順序連接而成奏司。可以設(shè)置多邊形頂點(diǎn)樟插、描邊的寬度和顏色韵洋、多邊形的填充色、層級(jí)關(guān)系黄锤、可見性搪缨、可點(diǎn)擊性等屬性。
常用屬性
類型 |
方法 |
說明 |
PolygonOptions |
strokeColor (int strokeColor) |
設(shè)置PolygonOptions描邊顏色, 與 texture(BitmapDescriptor) 互斥 |
PolygonOptions |
strokeWidth (float strokeWidth) |
設(shè)置PolygonOptions描邊寬度 |
PolygonOptions |
fillColor (int fillColor) |
設(shè)置PolygonOptions填充顏色 |
PolygonOptions |
add (LatLng... latLngs) |
向PolygonOptions添加頂點(diǎn) |
PolygonOptions |
add (LatLng latLng) |
向PolygonOptions添加頂點(diǎn) |
PolygonOptions |
add (List< LatLng > points) |
添加頂點(diǎn)坐標(biāo) |
PolygonOptions |
addAll (Iterable< LatLng > latLngs) |
向PolygonOptions添加頂點(diǎn) |
交互
類型 |
方法 |
說明 |
PolygonOptions |
clickable (boolean clickable) |
多邊形是否支持點(diǎn)擊 |
其它屬性
類型 |
方法 |
說明 |
PolygonOptions |
setHolePoints (List< List < LatLng > > holePoints) |
設(shè)置PolygonOptions 帶洞頂點(diǎn) |
PolygonOptions |
visible (boolean flag) |
設(shè)置折線可見性 |
PolygonOptions |
zIndex (int zIndex) |
設(shè)置相同顯示Level level(int) 的多邊形堆疊順序猜扮,相同顯示level勉吻,zIndex越大越靠上顯示 level優(yōu)先級(jí)大于zIndex |
PolygonOptions |
level (int level) |
設(shè)置Polygon的顯示Level,level用于控制Polygon與樓塊旅赢,道路齿桃,POI的顯示層級(jí)關(guān)系惑惶。 |
PolygonOptions |
pattern (List< Integer > pattern) |
設(shè)置ARGB虛線的樣式,與 texture(BitmapDescriptor) 互斥 |
PolygonOptions |
texture (BitmapDescriptor texture) |
設(shè)置紋理圖片短纵,這個(gè)紋理會(huì)重復(fù)地繪填充到線上带污,同時(shí)用戶應(yīng)配置紋理間隔 textureSpacing(int), 與 strokeColor(int)香到、pattern(List)兩個(gè)配置互斥鱼冀,這三個(gè)接口最后調(diào)用的會(huì)生效 |
PolygonOptions |
textureSpacing (int textureSpacing) |
紋理間隔 |
Arc的更多屬性
弧線是由一組經(jīng)緯度點(diǎn)和夾角角度繪制而成,在地圖上繪制弧線由 Arc
類定義實(shí)現(xiàn)悠就。
- 基礎(chǔ)圓弧途徑點(diǎn)必須在起終點(diǎn)有效坐標(biāo)范圍內(nèi)千绪,否則不能生成正確的弧線,同時(shí)設(shè)置夾角角度時(shí)梗脾,以夾角角度為準(zhǔn)荸型。
- 如果使用的是起終點(diǎn)繪制弧線,一定要添加夾角角度炸茧。
類型 |
方法 |
說明 |
ArcOptions |
color (int color) |
設(shè)置線顏色瑞妇,默認(rèn)黑色 |
ArcOptions |
width (float width) |
設(shè)置線寬,默認(rèn)5 |
ArcOptions |
strokeColor (int strokeColor) |
設(shè)置描邊顏色梭冠,不設(shè)置不顯示 |
ArcOptions |
strokeWidth (float strokeWidth) |
設(shè)置描邊寬度辕狰,默認(rèn)1 |
ArcOptions |
points (LatLng start, LatLng end) |
設(shè)置起終點(diǎn)坐標(biāo) |
ArcOptions |
angle (float angle) |
設(shè)置起點(diǎn)到終點(diǎn),與起點(diǎn)外切線逆時(shí)針旋轉(zhuǎn)的夾角角度 通過設(shè)置起終點(diǎn)+夾角角度控漠,即可確定一個(gè)圓弧線蔓倍,如果同時(shí)設(shè)置途經(jīng)點(diǎn)和夾角時(shí),優(yōu)先以夾角角度為準(zhǔn) |
ArcOptions |
pass (LatLng pass) |
設(shè)置途經(jīng)點(diǎn) 通過設(shè)置起終點(diǎn)+途經(jīng)點(diǎn)润脸,即可確定一個(gè)圓弧線柬脸,途經(jīng)點(diǎn)必須在起終點(diǎn)有效坐標(biāo)范圍內(nèi)他去,否則不能生成正確的弧線毙驯,同時(shí)設(shè)置夾角角度時(shí),以夾角角度為準(zhǔn) |
ArcOptions |
showArrow (boolean showArrow) |
設(shè)置箭頭顯示狀態(tài)灾测,默認(rèn)為false爆价,不顯示 |
Circle的更多屬性
圓形是由 Circle
類定義的封閉曲線,在騰訊地圖構(gòu)造一個(gè)圓形需要確定它的圓心和半徑媳搪∶危可以設(shè)置圓心坐標(biāo)、半徑秦爆、描邊的寬度和顏色序愚、圓的填充顏色、層級(jí)等限、可見性爸吮、可點(diǎn)擊性等屬性芬膝。
類型 |
方法 |
說明 |
CircleOptions |
radius (double radius) |
設(shè)置圓的半徑, 單位為米 |
CircleOptions |
fillColor (int color) |
設(shè)置圓的填充顏色 |
CircleOptions |
strokeColor (int color) |
設(shè)置圓的描邊顏色 |
CircleOptions |
strokeWidth (float width) |
設(shè)置圓的描邊寬度 |
CircleOptions |
borderType (CircleOptions.CircleBorderType borderType) |
設(shè)置描邊類型 |
CircleOptions |
pattern (List< Integer > pattern) |
設(shè)置描邊虛線的樣式 |
CircleOptions |
center (LatLng latlng) |
設(shè)置圓心坐標(biāo) |
CircleOptions |
clickable (boolean clickable) |
圓形是否支持點(diǎn)擊 |
CircleOptions |
visible (boolean flag) |
設(shè)置圓的可見性 |
CircleOptions |
zIndex (int zIndex) |
設(shè)置相同顯示Level level(int) 的Circle的堆疊順序形娇,相同顯示level锰霜,zIndex越大越靠上顯示 |
CircleOptions |
level (int level) |
設(shè)置Circle的顯示Level,level用于控制Circle與樓塊桐早,道路癣缅,POI的顯示層級(jí)關(guān)系。 |