標(biāo)記
用戶可以在地圖的指定位置添加標(biāo)記以標(biāo)識位置、商家、建筑等,并可以通過信息窗口展示詳細(xì)信息。
添加標(biāo)記
使用默認(rèn)圖標(biāo)在地圖上添加一個簡單的標(biāo)記。
示例代碼如下:
private Marker mMarker; public void addMarker(View view) { if (null != mMarker) { mMarker.remove(); } MarkerOptions options = new MarkerOptions() .position(new LatLng(
48.893478
,2.334595
)) .title("Hello Huawei Map
") .snippet("This is a snippet!
"); mMarker = hMap.addMarker(options);}private var mMarker: Marker? = null fun addMarker(view: View?) { if (null != mMarker) { mMarker?.remove() } val options = MarkerOptions() .position(LatLng(
48.893478
,2.334595
)) .title("Hello Huawei Map
") .snippet("This is a snippet!
") mMarker = hMap.addMarker(options)}您可以自定義圖像代替默認(rèn)圖標(biāo),還可以設(shè)置標(biāo)記屬性來改變圖標(biāo)。標(biāo)記的以下屬性支持自定義:
方法
功能
position(LatLng latlng)
標(biāo)記在地圖上的經(jīng)緯度,這是Marker對象唯一必需的屬性。
rotation(float rotation)
標(biāo)記在地圖上的旋轉(zhuǎn)角度。
title(String title)
用戶點(diǎn)按標(biāo)記時在信息窗口中顯示的字符串。
snippet(String snippet)
標(biāo)題文本框顯示的其他文字。
icon(BitmapDescriptor iconDescriptor)
代替默認(rèn)標(biāo)記圖像。
visible(boolean visible)
標(biāo)記的可見性。
true:可見 false:不可見默認(rèn)值為true。
zIndex(float zIndex)
標(biāo)記的z指數(shù)。
anchorMarker(float u, float v)
標(biāo)記的錨點(diǎn)位置。
draggable(boolean draggable)
true:允許用戶移動標(biāo)記。 false:禁止用戶移動標(biāo)記。默認(rèn)值為false。
alpha(float alpha)
標(biāo)記的透明度,取值范圍:[0, 1]。
0:完全透明 1:完全不透明flat(boolean flat)
標(biāo)記是否平貼地圖。
true:平貼地圖 false:面對相機(jī)默認(rèn)值為false。
infoWindowAnchor(float u, float v)
標(biāo)記信息窗口的錨點(diǎn)坐標(biāo)。
自定義標(biāo)記圖像
您可以使用自定義圖像(通常稱為圖標(biāo))修改默認(rèn)標(biāo)記圖標(biāo),通過調(diào)用BitmapDescriptorFactory類中的靜態(tài)方法生成BitmapDescriptor對象(圖片描述文件)。
方法
功能
fromAsset(String assetName)
使用assets目錄中的Bitmap圖像創(chuàng)建自定義標(biāo)記圖標(biāo)。
fromBitmap(Bitmap image)
通過位圖圖像創(chuàng)建自定義標(biāo)記圖標(biāo)。
fromFile(String fileName)
使用位于內(nèi)部存儲中的位圖文件創(chuàng)建自定義標(biāo)記圖標(biāo)。
fromPath(String absolutePath)
通過位圖的絕對文件路徑創(chuàng)建自定義標(biāo)記圖標(biāo)。
fromResource(int resourceId)
使用位圖圖像的資源創(chuàng)建自定義標(biāo)記圖標(biāo)。
創(chuàng)建一個帶有自定義圖標(biāo)的標(biāo)記。
示例代碼如下:
private Marker mMarker; mMarker = hMap.addMarker(new MarkerOptions() .position(new LatLng(
48.893478
,2.334595
)) .icon(BitmapDescriptorFactory.fromResource(R.drawable.badge_ph)));private var mMarker: Marker? = null mMarker = hMap.addMarker(MarkerOptions() .position(LatLng(
48.893478
,2.334595
)) .icon(BitmapDescriptorFactory.fromResource(R.drawable.badge_ph)))修改標(biāo)記
地圖SDK支持在添加標(biāo)記之后,修改已經(jīng)設(shè)置的標(biāo)記屬性。
示例代碼如下:
if (mMarker != null) { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.badge_tr); BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap); mMarker.setIcon(bitmapDescriptor);} if (mMarker != null) { mMarker.setTitle("
Marker title
");} ... if (mMarker != null) { mMarker.setDraggable(true
);} if (mMarker != null) { mMarker.setMarkerAnchor(0.9F
,0.9F
);}if (mMarker != null) { val bitmap = BitmapFactory.decodeResource(resources, R.drawable.badge_tr) val bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap) mMarker?.setIcon(bitmapDescriptor)} if (mMarker != null) { mMarker?.title = "
Marker title
"} ... if (mMarker != null) { mMarker?.isDraggable =true
} if (mMarker != null) { mMarker?.setMarkerAnchor(0.9f
,0.9f
)}聚合標(biāo)記
地圖SDK支持聚合標(biāo)記功能,該功能可以有效地管理在地圖不同縮放級別情況下的多個標(biāo)記,使用該功能需要標(biāo)記數(shù)量大于等于5個,在小于5個時不支持聚合標(biāo)記。當(dāng)以高縮放級別查看地圖時,各個標(biāo)記會顯示在地圖上(圖1);以較低級別查看地圖時,標(biāo)記會聚集成集群,從而使標(biāo)記的呈現(xiàn)更有序(圖2)。
可以參見以下示例代碼實(shí)現(xiàn)標(biāo)記聚合功能:
示例代碼如下:
@Overridepublic void onMapReady(HuaweiMap map) { hMap = map; hMap.moveCamera(CameraUpdateFactory.newLatLngZoom( new LatLng(
48.893478
,2.334595
),10
)); hMap.addMarker(new MarkerOptions().position(new LatLng(48.891478
,2.334595
)).title("Marker1
").clusterable(true
)); hMap.addMarker(new MarkerOptions().position(new LatLng(48.892478
,2.334595
)).title("Marker2
").clusterable(true
)); hMap.addMarker(new MarkerOptions().position(new LatLng(48.893478
,2.334595
)).title("Marker3
").clusterable(true
)); hMap.addMarker(new MarkerOptions().position(new LatLng(48.894478
,2.334595
)).title("Marker4
").clusterable(true
)); hMap.addMarker(new MarkerOptions().position(new LatLng(48.895478
,2.334595
)).title("Marker5
").clusterable(true
)); hMap.addMarker(new MarkerOptions().position(new LatLng(48.896478
,2.334595
)).title("Marker6
").clusterable(true
)); hMap.setMarkersClustering(true
);}override fun onMapReady(map: HuaweiMap) { hMap = map hMap.moveCamera(CameraUpdateFactory.newLatLngZoom( LatLng(
48.893478
,2.334595
),10f
)) hMap.addMarker(MarkerOptions().position(LatLng(48.891478
,2.334595
)).title("Marker1
").clusterable(true
)) hMap.addMarker(MarkerOptions().position(LatLng(48.892478
,2.334595
)).title("Marker2
").clusterable(true
)) hMap.addMarker(MarkerOptions().position(LatLng(48.893478
,2.334595
)).title("Marker3
").clusterable(true
)) hMap.addMarker(MarkerOptions().position(LatLng(48.894478
,2.334595
)).title("Marker4
").clusterable(true
)) hMap.addMarker(MarkerOptions().position(LatLng(48.895478
,2.334595
)).title("Marker5
").clusterable(true
)) hMap.addMarker(MarkerOptions().position(LatLng(48.896478
,2.334595
)).title("Marker6
").clusterable(true
)) hMap.setMarkersClustering(true
)}圖1 高縮放級別下多標(biāo)記展示
圖2 低縮放級別下多標(biāo)記展示
自定義聚合標(biāo)記
聚合標(biāo)記支持您自定義聚合圖標(biāo)顏色、圖片及文本顏色。
設(shè)置默認(rèn)聚合圖標(biāo)的顏色
默認(rèn)情下,聚合圖標(biāo)為藍(lán)色,修改聚合圖標(biāo)的顏色,請調(diào)用UiSettings.setMarkerClusterColor(int color)設(shè)置。
示例代碼如下:
hMap.getUiSettings().setMarkerClusterColor(Color.RED);
hMap.uiSettings.setMarkerClusterColor(Color.RED)
自定義聚合圖標(biāo)
地圖SDK支持自定義聚合圖標(biāo),請調(diào)用UiSettings.setMarkerClusterIcon(BitmapDescriptor iconDescriptor)設(shè)置。
示例代碼如下:
hMap.getUiSettings().setMarkerClusterIcon(BitmapDescriptorFactory.fromResource(R.drawable.avocado));
hMap.uiSettings.setMarkerClusterIcon(BitmapDescriptorFactory.fromResource(R.drawable.avocado))
自定義聚合圖標(biāo)的文本顏色
地圖SDK支持自定義聚合圖標(biāo)的文本顏色,請調(diào)用UiSettings.setMarkerClusterTextColor(int color)設(shè)置。
示例代碼如下:
hMap.getUiSettings().setMarkerClusterTextColor(Color.RED);
hMap.uiSettings.setMarkerClusterTextColor(Color.RED)
標(biāo)記事件
標(biāo)記點(diǎn)擊事件
可以使用HuaweiMap.OnMarkerClickListener來偵聽標(biāo)記上的點(diǎn)擊事件。要在地圖上設(shè)置此偵聽器,請調(diào)用HuaweiMap對象的setOnMarkerClickListener(HuaweiMap.OnMarkerClickListener)方法。當(dāng)用戶點(diǎn)擊標(biāo)記時,onMarkerClick(Marker)將調(diào)用該標(biāo)記并將標(biāo)記作為參數(shù)傳遞。標(biāo)記點(diǎn)擊事件的默認(rèn)行為是顯示其信息窗口(如果可用),使標(biāo)記在地圖上居中。
示例代碼如下:
hMap.setOnMarkerClickListener(new HuaweiMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { Toast.makeText(getApplicationContext(), "onMarkerClick:" + marker.getTitle(), Toast.LENGTH_SHORT).show(); return false; }});
hMap.setOnMarkerClickListener { marker -> Toast.makeText(applicationContext, "onMarkerClick:${marker.title}", Toast.LENGTH_SHORT).show() false}
標(biāo)記拖動事件
可以使用HuaweiMap.OnMarkerDragListener來偵聽標(biāo)記上的拖動事件,前提是該Marker標(biāo)記已設(shè)置拖拽屬性為true。要在地圖上設(shè)置此偵聽器,請調(diào)用HuaweiMap對象的setOnMarkerDragListener(HuaweiMap.OnMarkerDragListener)方法。要拖動標(biāo)記,用戶必須長按標(biāo)記。當(dāng)用戶將手指從屏幕上移開時,標(biāo)記將保持在該位置。拖動標(biāo)記時,先調(diào)用onMarkerDragStart(Marker),標(biāo)記拖動過程中,不斷調(diào)用onMarkerDrag(Marker)。拖動結(jié)束時,調(diào)用onMarkerDragEnd(Marker)。您可以隨時通過調(diào)用Marker的getPosition()接口獲取標(biāo)記的位置。
示例代碼如下:
mMarker.setDraggable(
true
); hMap.setOnMarkerDragListener(new HuaweiMap.OnMarkerDragListener() { @Override public void onMarkerDragStart(Marker marker) { Log.i(TAG, "onMarkerDragStart: "); } @Override public void onMarkerDrag(Marker marker) { Log.i(TAG, "onMarkerDrag: "); } @Override public void onMarkerDragEnd(Marker marker) { Log.i(TAG, "onMarkerDragEnd: "); }});mMarker?.isDraggable =
true
hMap.setOnMarkerDragListener(object : OnMarkerDragListener { override fun onMarkerDragStart(marker: Marker) { Log.i(TAG, "onMarkerDragStart: ") } override fun onMarkerDrag(marker: Marker) { Log.i(TAG, "onMarkerDrag: ") } override fun onMarkerDragEnd(marker: Marker) { Log.i(TAG, "onMarkerDragEnd: ") }})標(biāo)記動畫
地圖SDK提供給標(biāo)記設(shè)置動畫效果的方法,涉及到的類有:
示例代碼如下:
Marker mParis;mParis = hMap.addMarker(new MarkerOptions().position(new LatLng(
48.893478
,2.334595
)).title("paris
").snippet("hello
")); Animation alphaAnimation = new AlphaAnimation(0.2f
,1.0f
);alphaAnimation.setRepeatCount(5
);alphaAnimation.setDuration(1000L
);alphaAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart() { Log.d(TAG, "Alpha Animation Start"); } @Override public void onAnimationEnd() { Log.d(TAG, "Alpha Animation End"); }}); Animation scaleAnimation = new ScaleAnimation(0, 2, 0, 2
);scaleAnimation.setRepeatCount(10
);scaleAnimation.setDuration(1000L
);scaleAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart() { Log.d(TAG, "Scale Animation Start"); } @Override public void onAnimationEnd() { Log.d(TAG, "Scale Animation End"); }}); AnimationSet animationSet = new AnimationSet(true
);animationSet.setInterpolator(new LinearInterpolator());animationSet.addAnimation(alphaAnimation);animationSet.addAnimation(scaleAnimation); mParis.setAnimation(animationSet);mParis.startAnimation();val mParis: Marker = hMap.addMarker(MarkerOptions().position(LatLng(
48.893478
,2.334595
)).title("paris
").snippet("hello
")) val alphaAnimation: Animation = AlphaAnimation(0.2f
,1.0f
)alphaAnimation.repeatCount =5
alphaAnimation.duration =1000L
alphaAnimation.setAnimationListener(object : Animation.AnimationListener { override fun onAnimationStart() { Log.d(TAG, "Alpha Animation Start") } override fun onAnimationEnd() { Log.d(TAG, "Alpha Animation End") }}) val scaleAnimation: Animation = ScaleAnimation(0f, 2f, 0f, 2f
)scaleAnimation.repeatCount =10
scaleAnimation.duration =1000L
scaleAnimation.setAnimationListener(object : Animation.AnimationListener { override fun onAnimationStart() { Log.d(TAG, "Scale Animation Start") } override fun onAnimationEnd() { Log.d(TAG, "Scale Animation End") }}) val animationSet = AnimationSet(true
)animationSet.interpolator = LinearInterpolator()animationSet.addAnimation(alphaAnimation)animationSet.addAnimation(scaleAnimation) mParis.setAnimation(animationSet)mParis.startAnimation()信息窗
信息窗在標(biāo)記上方的彈出窗口中顯示文本或圖像,為標(biāo)記提供詳細(xì)信息。
添加信息窗
添加信息窗的最簡單方法是設(shè)置相應(yīng)MarkerOptions對象的title()和snippet()方法。設(shè)置這些屬性將導(dǎo)致在點(diǎn)擊該標(biāo)記時顯示信息窗。
示例代碼如下:
public void addMarker(View view) { if (null != mMarker) { mMarker.remove(); } MarkerOptions options = new MarkerOptions().position(new LatLng(
48.893478
,2.334595
)); options.title("Hello Huawei Map
"); options.snippet("This is a snippet!
"); mMarker = hMap.addMarker(options);}fun addMarker(view: View?) { if (null != mMarker) { mMarker?.remove() } val options = MarkerOptions().position(LatLng(
48.893478
,2.334595
)) options.title("Hello Huawei Map
") options.snippet("This is a snippet!
") mMarker = hMap.addMarker(options)}顯示/隱藏信息窗
信息窗旨在響應(yīng)用戶觸摸事件??梢酝ㄟ^調(diào)用Marker對象的showInfoWindow()顯示信息窗,可以通過調(diào)用Marker對象的hideInfoWindow()隱藏信息窗。
示例代碼如下:
boolean isInfoWindowShown = mMarker.isInfoWindowShown();if (isInfoWindowShown) { mMarker.hideInfoWindow();} else { mMarker.showInfoWindow();}
val isInfoWindowShown: Boolean? = mMarker?.isInfoWindowShownif (isInfoWindowShown != null && isInfoWindowShown) { mMarker?.hideInfoWindow()} else { mMarker?.showInfoWindow()}
自定義信息窗
可以自定義信息窗的內(nèi)容。為此,您必須創(chuàng)建HuaweiMap.InfoWindowAdapter接口的具體實(shí)現(xiàn),然后HuaweiMap對象的setInfoWindowAdapter()使用您的實(shí)現(xiàn)進(jìn)行調(diào)用 。該接口包含兩種實(shí)現(xiàn)方法:getInfoWindow()和getInfoContents(),這兩種方法會按照書寫順序調(diào)用。
如果僅僅自定義信息窗格式,則需要實(shí)現(xiàn)getInfoWindow(),getInfoContents()返回null;如果僅僅自定義信息窗內(nèi)容,則需要實(shí)現(xiàn)getInfoContents(),getInfoWindow()返回null;如果信息窗口和信息窗口內(nèi)容都自定義,則getInfoWindow(Marker)和getInfoContents()都需要實(shí)現(xiàn)。設(shè)計(jì)自定義信息窗的步驟如下:
設(shè)置窗口或者內(nèi)容的布局文件custom_info_window.xml或custom_info_contents.xml,實(shí)例代碼僅演示getInfoWindow()方法的實(shí)現(xiàn)。<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http: android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:adjustViewBounds="true" android:src="@drawable/orange" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/txtv_titlee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:ellipsize="end" android:singleLine="true" android:textColor="#ff000000" android:textSize="14sp" android:textStyle="bold" /> <TextView android:id="@+id/txtv_snippett" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:singleLine="true" android:textColor="#ff7f7f7f" android:textSize="14sp" /> </LinearLayout></LinearLayout> 實(shí)現(xiàn)HuaweiMap.InfoWindowAdapter。
示例代碼如下:
class CustomInfoWindowAdapter implements HuaweiMap.InfoWindowAdapter { private final View mWindow; CustomInfoWindowAdapter() { mWindow = getLayoutInflater().inflate(R.layout.custom_info_window, null); } @Override public View getInfoWindow(Marker marker) { TextView txtvTitle = mWindow.findViewById(R.id.txtv_titlee); TextView txtvSnippett = mWindow.findViewById(R.id.txtv_snippett); txtvTitle.setText("
Paris
"); txtvSnippett.setText("hello
"); return mWindow; } @Override public View getInfoContents(Marker marker) { return null; }} hMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());internal inner class CustomInfoWindowAdapter : HuaweiMap.InfoWindowAdapter { private val mWindow: View init { mWindow = layoutInflater.inflate(R.layout.custom_info_window, null) } override fun getInfoWindow(marker: Marker): View { val txtvTitle = mWindow.findViewById<TextView>(R.id.txtv_titlee) val txtvSnippett = mWindow.findViewById<TextView>(R.id.txtv_snippett) txtvTitle.text = "
Paris
" txtvSnippett.text = "hello
" return mWindow } override fun getInfoContents(marker: Marker): View? { return null }} hMap.setInfoWindowAdapter(CustomInfoWindowAdapter())地圖SDK支持兩種信息窗口:默認(rèn)信息窗口、自定義信息窗口,如圖3和圖4所示。
圖3 默認(rèn)信息窗口
圖4 自定義信息窗口
相關(guān)知識
過敏史在()標(biāo)記
夏季養(yǎng)生指南老人要記健康日記 記身體指標(biāo)
標(biāo)準(zhǔn)體重記錄軟件下載
身體指標(biāo)記錄APP
健康日記 HEALTHDIARY商標(biāo)
開評標(biāo)健康信息登記表.docx
牢記健康的完美指標(biāo) 10指標(biāo)自測健康度
茶葉質(zhì)量檢驗(yàn)與記錄標(biāo)準(zhǔn)指南
國家學(xué)生體質(zhì)健康標(biāo)準(zhǔn)登記卡
《國家學(xué)生體質(zhì)健康標(biāo)準(zhǔn)》登記卡
網(wǎng)址: 標(biāo)記 http://m.u1s5d6.cn/newsview1197626.html
推薦資訊
- 1發(fā)朋友圈對老公徹底失望的心情 12775
- 2BMI體重指數(shù)計(jì)算公式是什么 11235
- 3補(bǔ)腎吃什么 補(bǔ)腎最佳食物推薦 11199
- 4性生活姿勢有哪些 盤點(diǎn)夫妻性 10428
- 5BMI正常值范圍一般是多少? 10137
- 6在線基礎(chǔ)代謝率(BMR)計(jì)算 9652
- 7一邊做飯一邊躁狂怎么辦 9138
- 8從出汗看健康 出汗透露你的健 9063
- 9早上怎么喝水最健康? 8613
- 10五大原因危害女性健康 如何保 7828