標籤:
下文為各位重點介紹關於Android高德地圖自訂Markers的例子,希望這篇文章能夠讓各位理解到Android高德地圖自訂Markers的方法。
之前的部落格裡說了地圖的嵌入和定位,今天就說說在地圖上顯示一些我們想要的。在地圖中有內建的Markers(標記),但是它只顯示一個橢圓的表徵圖,一般是不符合我們的需求的,這樣就要我們自己來自訂。首先標記有下面一些屬性;
1.position(Required) 在地圖上標記位置的經緯度值。參數不可為空。
2.title 當使用者點擊標記,在資訊視窗上顯示的字串。
3.snippet 附加文本,顯示在標題下方。
4.draggable 如果您允許使用者可以自由移動標記,設定為“ true ”。預設情況下為“ false ”。
5.visible 設定“ false ”,標記不可見。預設情況下為“ true ”。
6.anchor表徵圖擺放在地圖上的基準點。預設情況下,錨點是從圖片下沿的中間處。
7.perspective設定 true,標記有近大遠小效果。預設情況下為 false。
8.可以通過Marker.setRotateAngle() 方法設定標記的旋轉角度,從正北開始,逆時針計算。如設定旋轉90度,Marker.setRotateAngle(90)
9.通過setFlat() 方法設定標誌是否貼地顯示
自訂表徵圖通常由 BitmapDescriptor 設定。我們可以在類 BitmapDescriptorFactory 使用以下其中一種方法定義。
1.fromAsset(String assetName) 在 assets 目錄中使用映像建立自訂標籤。
2.fromBitmap (Bitmap image) 使用位元影像映像建立自訂標籤。
3.fromFile (String path) 指定路徑的檔案建立自訂表徵圖。
4.fromResource (int resourceId) 使用已經存在的資源建立自訂表徵圖。先看一下要實現的效果:
地圖內建標記 實現效果
實現思路是:自訂布局,擷取資料填入相應位置,然後將view轉成Bitmap,調用AMap.addMarker(markerOptions) 方法添加到地圖上。
自訂布局並填充資料:
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.轉成Bitmap:
//view 轉bitmappublic 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座標點位置(example:LatLng point = new LatLng(39.963175, * 116.400244); ) * @param markerIcon 表徵圖 * @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; }
這樣就實現了上述效果。
Android高德地圖自訂Markers的例子