android 自定义地图标注,Android高德地图自定义Markers【原创】
之前的博客里說了地圖的嵌入和定位,今天就說說在地圖上顯示一些我們想要的。在地圖中有自帶的Markers(標(biāo)記),但是它只顯示一個橢圓的圖標(biāo),一般是不符合我們的需求的,這樣就要我們自己來自定義。首先標(biāo)記有下面一些屬性;
1.position(Required) 在地圖上標(biāo)記位置的經(jīng)緯度值。參數(shù)不能為空。
2.title 當(dāng)用戶點(diǎn)擊標(biāo)記,在信息窗口上顯示的字符串。
3.snippet 附加文本,顯示在標(biāo)題下方。
4.draggable 如果您允許用戶可以自由移動標(biāo)記,設(shè)置為“ true ”。默認(rèn)情況下為“ false ”。
5.visible 設(shè)置“ false ”,標(biāo)記不可見。默認(rèn)情況下為“ true ”。
6.anchor圖標(biāo)擺放在地圖上的基準(zhǔn)點(diǎn)。默認(rèn)情況下,錨點(diǎn)是從圖片下沿的中間處。
7.perspective設(shè)置 true,標(biāo)記有近大遠(yuǎn)小效果。默認(rèn)情況下為 false。
8.可以通過Marker.setRotateAngle() 方法設(shè)置標(biāo)記的旋轉(zhuǎn)角度,從正北開始,逆時針計算。如設(shè)置旋轉(zhuǎn)90度,Marker.setRotateAngle(90)
9.通過setFlat() 方法設(shè)置標(biāo)志是否貼地顯示
自定義圖標(biāo)通常由 BitmapDescriptor 設(shè)置。我們可以在類 BitmapDescriptorFactory 使用以下其中一種方法定義。
1.fromAsset(String assetName) 在 assets 目錄中使用圖像創(chuàng)建自定義標(biāo)記。
2.fromBitmap (Bitmap image) 使用位圖圖像創(chuàng)建自定義標(biāo)記。
3.fromFile (String path) 指定路徑的文件創(chuàng)建自定義圖標(biāo)。
4.fromResource (int resourceId) 使用已經(jīng)存在的資源創(chuàng)建自定義圖標(biāo)。
先看一下要實(shí)現(xiàn)的效果:
?地圖自帶標(biāo)記 ? ? ? ? ? ? ? ? ? ??實(shí)現(xiàn)效果
實(shí)現(xiàn)思路是:自定義布局,獲取數(shù)據(jù)填入相應(yīng)位置,然后將view轉(zhuǎn)成Bitmap,調(diào)用AMap.addMarker(markerOptions) 方法添加到地圖上。
1.自定義布局并填充數(shù)據(jù):
for (int i = 0; i < positionEneityList.size(); i++) {
if (positionEneityList.get(i).getType().equals("1")) {
View view = View.inflate(getActivity(),R.layout.view_day, null);
TextView tv_price = (TextView) view.findViewById(R.id.tv_price);
TextView tv_price_status = (TextView) view.findViewById(R.id.tv_price_status);
tv_price.setText(positionEneityList.get(i).getPrice());
tv_price_status.setText("元/時");
Bitmap bitmap = CommentActivity.convertViewToBitmap(view);
drawMarkerOnMap(new LatLng(Double.parseDouble(positionEneityList.get(i).getLatitude())
, Double.parseDouble(positionEneityList.get(i).getLongitude())), bitmap, positionEneityList.get(i).getId());
}
}
2.轉(zhuǎn)成Bitmap:
//view 轉(zhuǎn)bitmap
public static Bitmap convertViewToBitmap(View view) {
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
3.添加到地圖上:
/**
* 在地圖上畫marker
*
* @param point marker坐標(biāo)點(diǎn)位置(example:LatLng point = new LatLng(39.963175,
* 116.400244); )
* @param markerIcon 圖標(biāo)
* @return Marker對象
*/
private Marker drawMarkerOnMap(LatLng point, Bitmap markerIcon, String id) {
if (aMap != null && point != null) {
Marker marker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 1)
.position(point)
.title(id)
.icon(BitmapDescriptorFactory.fromBitmap(markerIcon)));
return marker;
}
return null;
}
這樣就實(shí)現(xiàn)了上述效果。
轉(zhuǎn)載時請注明出處及相應(yīng)鏈接,本文永久地址:https://blog.yayuanzi.com/13791.html
微信打賞
支付寶打賞
感謝您對作者Lena的打賞,我們會更加努力!????如果您想成為作者,請點(diǎn)我
總結(jié)
以上是生活随笔為你收集整理的android 自定义地图标注,Android高德地图自定义Markers【原创】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么同事写的代码那么优雅~
- 下一篇: 前端学习(3185):ant-desig