2016-08-18 3 views
0

マップ上に動的カスタムマーカーを設定しようとしていますが、機能していない代わりにデフォルトのマーカーが表示されています。私はここでどこが間違っていますか?MapBoxカスタムマーカーが機能しない

IconFactory iconFactory = IconFactory.getInstance(MainActivity.this); 
TextDrawable iconDrawable = new TextDrawable("CD"); 
Icon icon = iconFactory.fromDrawable(iconDrawable); 

MarkerOptions marker = new MarkerOptions() 
    .position(new LatLng(location.latitude, location.longitude)) 
    .icon(icon) 

mapboxMap.addMarker(marker); 

TextDrawable:

public class TextDrawable extends Drawable { 

    private final String text; 
    private final Paint paint; 

    public TextDrawable(String text) { 

     this.text = text; 

     this.paint = new Paint(); 
     paint.setColor(Color.WHITE); 
     paint.setTextSize(22f); 
     paint.setAntiAlias(true); 
     paint.setFakeBoldText(true); 
     paint.setStrokeWidth(2); 
     paint.setStyle(Paint.Style.FILL); 
     paint.setTextAlign(Paint.Align.LEFT); 
    } 

    @Override 
    public void draw(Canvas canvas) { 
     canvas.drawText(text, 0, 0, paint); 
    } 

    @Override 
    public void setAlpha(int alpha) { 
     paint.setAlpha(alpha); 
    } 

    @Override 
    public void setColorFilter(ColorFilter cf) { 
     paint.setColorFilter(cf); 
    } 

    @Override 
    public int getOpacity() { 
     return PixelFormat.TRANSLUCENT; 
    } 
} 

答えて

1

使用このコード:

Marker myLocMarker = map.addMarker(new MarkerOptions() 
      .position(myLocation) 
      .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.bluebox, "your text goes here")))); 

ブルーボックスは、我々は感謝フアン背景

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

} 
+0

として使用するだけで、長方形の資源であります!正確に私が必要としたもの。 – doovers

関連する問題