首頁 資訊 標記

標記

來源:泰然健康網(wǎng) 時間:2025年05月01日 04:24

用戶可以在地圖的指定位置添加標記以標識位置、商家、建筑等,并可以通過信息窗口展示詳細信息。

添加標記

使用默認圖標在地圖上添加一個簡單的標記。

示例代碼如下:

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)}

您可以自定義圖像代替默認圖標,還可以設(shè)置標記屬性來改變圖標。標記的以下屬性支持自定義:

方法

功能

position(LatLng latlng)

標記在地圖上的經(jīng)緯度,這是Marker對象唯一必需的屬性。

rotation(float rotation)

標記在地圖上的旋轉(zhuǎn)角度。

title(String title)

用戶點按標記時在信息窗口中顯示的字符串。

snippet(String snippet)

標題文本框顯示的其他文字。

icon(BitmapDescriptor iconDescriptor)

代替默認標記圖像。

visible(boolean visible)

標記的可見性。

true:可見 false:不可見

默認值為true。

zIndex(float zIndex)

標記的z指數(shù)。

anchorMarker(float u, float v)

標記的錨點位置。

draggable(boolean draggable)

true:允許用戶移動標記。 false:禁止用戶移動標記。

默認值為false。

alpha(float alpha)

標記的透明度,取值范圍:[0, 1]。

0:完全透明 1:完全不透明

flat(boolean flat)

標記是否平貼地圖。

true:平貼地圖 false:面對相機

默認值為false。

infoWindowAnchor(float u, float v)

標記信息窗口的錨點坐標。

自定義標記圖像

您可以使用自定義圖像(通常稱為圖標)修改默認標記圖標,通過調(diào)用BitmapDescriptorFactory類中的靜態(tài)方法生成BitmapDescriptor對象(圖片描述文件)。

方法

功能

fromAsset(String assetName)

使用assets目錄中的Bitmap圖像創(chuàng)建自定義標記圖標。

fromBitmap(Bitmap image)

通過位圖圖像創(chuàng)建自定義標記圖標。

fromFile(String fileName)

使用位于內(nèi)部存儲中的位圖文件創(chuàng)建自定義標記圖標。

fromPath(String absolutePath)

通過位圖的絕對文件路徑創(chuàng)建自定義標記圖標。

fromResource(int resourceId)

使用位圖圖像的資源創(chuàng)建自定義標記圖標。

創(chuàng)建一個帶有自定義圖標的標記。

示例代碼如下:

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)))

修改標記

地圖SDK支持在添加標記之后,修改已經(jīng)設(shè)置的標記屬性。

示例代碼如下:

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

)}

聚合標記

地圖SDK支持聚合標記功能,該功能可以有效地管理在地圖不同縮放級別情況下的多個標記,使用該功能需要標記數(shù)量大于等于5個,在小于5個時不支持聚合標記。當以高縮放級別查看地圖時,各個標記會顯示在地圖上(圖1);以較低級別查看地圖時,標記會聚集成集群,從而使標記的呈現(xiàn)更有序(圖2)。

可以參見以下示例代碼實現(xiàn)標記聚合功能:

示例代碼如下:

@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 高縮放級別下多標記展示

圖2 低縮放級別下多標記展示

自定義聚合標記

聚合標記支持您自定義聚合圖標顏色、圖片及文本顏色。

設(shè)置默認聚合圖標的顏色

默認情下,聚合圖標為藍色,修改聚合圖標的顏色,請調(diào)用UiSettings.setMarkerClusterColor(int color)設(shè)置。

示例代碼如下:

hMap.getUiSettings().setMarkerClusterColor(Color.RED);

hMap.uiSettings.setMarkerClusterColor(Color.RED)

自定義聚合圖標

地圖SDK支持自定義聚合圖標,請調(diào)用UiSettings.setMarkerClusterIcon(BitmapDescriptor iconDescriptor)設(shè)置。

示例代碼如下:

hMap.getUiSettings().setMarkerClusterIcon(BitmapDescriptorFactory.fromResource(R.drawable.avocado));

hMap.uiSettings.setMarkerClusterIcon(BitmapDescriptorFactory.fromResource(R.drawable.avocado))

自定義聚合圖標的文本顏色

地圖SDK支持自定義聚合圖標的文本顏色,請調(diào)用UiSettings.setMarkerClusterTextColor(int color)設(shè)置。

示例代碼如下:

hMap.getUiSettings().setMarkerClusterTextColor(Color.RED);

hMap.uiSettings.setMarkerClusterTextColor(Color.RED)

標記事件

標記點擊事件

可以使用HuaweiMap.OnMarkerClickListener來偵聽標記上的點擊事件。要在地圖上設(shè)置此偵聽器,請調(diào)用HuaweiMap對象的setOnMarkerClickListener(HuaweiMap.OnMarkerClickListener)方法。當用戶點擊標記時,onMarkerClick(Marker)將調(diào)用該標記并將標記作為參數(shù)傳遞。標記點擊事件的默認行為是顯示其信息窗口(如果可用),使標記在地圖上居中。

示例代碼如下:

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}

標記拖動事件

可以使用HuaweiMap.OnMarkerDragListener來偵聽標記上的拖動事件,前提是該Marker標記已設(shè)置拖拽屬性為true。要在地圖上設(shè)置此偵聽器,請調(diào)用HuaweiMap對象的setOnMarkerDragListener(HuaweiMap.OnMarkerDragListener)方法。要拖動標記,用戶必須長按標記。當用戶將手指從屏幕上移開時,標記將保持在該位置。拖動標記時,先調(diào)用onMarkerDragStart(Marker),標記拖動過程中,不斷調(diào)用onMarkerDrag(Marker)。拖動結(jié)束時,調(diào)用onMarkerDragEnd(Marker)。您可以隨時通過調(diào)用Marker的getPosition()接口獲取標記的位置。

示例代碼如下:

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: ") }})

標記動畫

地圖SDK提供給標記設(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()

信息窗

信息窗在標記上方的彈出窗口中顯示文本或圖像,為標記提供詳細信息。

添加信息窗

添加信息窗的最簡單方法是設(shè)置相應(yīng)MarkerOptions對象的title()和snippet()方法。設(shè)置這些屬性將導致在點擊該標記時顯示信息窗。

示例代碼如下:

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接口的具體實現(xiàn),然后HuaweiMap對象的setInfoWindowAdapter()使用您的實現(xiàn)進行調(diào)用 。該接口包含兩種實現(xiàn)方法:getInfoWindow()和getInfoContents(),這兩種方法會按照書寫順序調(diào)用。

如果僅僅自定義信息窗格式,則需要實現(xiàn)getInfoWindow(),getInfoContents()返回null;如果僅僅自定義信息窗內(nèi)容,則需要實現(xiàn)getInfoContents(),getInfoWindow()返回null;如果信息窗口和信息窗口內(nèi)容都自定義,則getInfoWindow(Marker)和getInfoContents()都需要實現(xiàn)。設(shè)計自定義信息窗的步驟如下:

設(shè)置窗口或者內(nèi)容的布局文件custom_info_window.xml或custom_info_contents.xml,實例代碼僅演示getInfoWindow()方法的實現(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> 實現(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支持兩種信息窗口:默認信息窗口、自定義信息窗口,如圖3和圖4所示。

圖3 默認信息窗口

圖4 自定義信息窗口

相關(guān)知識

過敏史在()標記
夏季養(yǎng)生指南老人要記健康日記 記身體指標
標準體重記錄軟件下載
身體指標記錄APP
健康日記 HEALTHDIARY商標
開評標健康信息登記表.docx
牢記健康的完美指標 10指標自測健康度
茶葉質(zhì)量檢驗與記錄標準指南
國家學生體質(zhì)健康標準登記卡
《國家學生體質(zhì)健康標準》登記卡

網(wǎng)址: 標記 http://www.u1s5d6.cn/newsview1197626.html

推薦資訊