Android高德地圖自訂Markers的例子

來源:互聯網
上載者:User

標籤:

下文為各位重點介紹關於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) 方法添加到地圖上。

  1. 自訂布局並填充資料:

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的例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.