亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

具有自定義標記的android Maps API v2

具有自定義標記的android Maps API v2

吃雞游戲 2019-12-25 11:14:11
我想用自定義標記制作地圖。在API v2中,我可以為標記設置圖標,標題等。但是我想在第一次發病時用標記顯示標題。現在,僅當我點擊標記時才顯示標題。在v1中有疊加層,但是在v2中我沒有發現任何類似的東西。編輯: 也許我不夠清楚。Marker.showInfoWindow()API中的某些功能僅適用于一個標記。我無法同時顯示所有標記的信息窗口。無論如何,我需要顯示所有標記的標題,而無需等待用戶點擊它。
查看完整描述

3 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

終于做到了。因此,您要做的是有一個背景圖像(在我的情況下,我僅使用一個藍色矩形)。像這樣創建一個標記:


Marker myLocMarker = map.addMarker(new MarkerOptions()

            .position(myLocation)

            .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.bluebox, "your text goes here"))));

請注意writeTextOnDrawable()方法:


private Bitmap writeTextOnDrawable(int drawableId, String text) {


    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId)

            .copy(Bitmap.Config.ARGB_8888, true);


    Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);


    Paint paint = new Paint();

    paint.setStyle(Style.FILL);

    paint.setColor(Color.WHITE);

    paint.setTypeface(tf);

    paint.setTextAlign(Align.CENTER);

    paint.setTextSize(convertToPixels(context, 11));


    Rect textRect = new Rect();

    paint.getTextBounds(text, 0, text.length(), textRect);


    Canvas canvas = new Canvas(bm);


    //If the text is bigger than the canvas , reduce the font size

    if(textRect.width() >= (canvas.getWidth() - 4))     //the padding on either sides is considered as 4, so as to appropriately fit in the text

        paint.setTextSize(convertToPixels(context, 7));        //Scaling needs to be used for different dpi's


    //Calculate the positions

    int xPos = (canvas.getWidth() / 2) - 2;     //-2 is for regulating the x position offset


    //"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.

    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;  


    canvas.drawText(text, xPos, yPos, paint);


    return  bm;

}




public static int convertToPixels(Context context, int nDP)

{

    final float conversionScale = context.getResources().getDisplayMetrics().density;


    return (int) ((nDP * conversionScale) + 0.5f) ;


}


查看完整回答
反對 回復 2019-12-25
  • 3 回答
  • 0 關注
  • 475 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號