8

Я хочу нарисовать текст в марке по умолчанию карты Google v2. Я сделал это, взяв свой собственный образ из drawable, но как я могу изменить его в марке по умолчанию.как рисовать текст по умолчанию маркер google map v2

мой код

marker.icon(BitmapDescriptorFactory 
      .fromBitmap(drawTextToBitmap(getApplicationContext(), R.drawable.images,"5"))); 

и drawTextToBitmap методов.

public static Bitmap drawTextToBitmap(Context gContext,int gResId,String gText) { 
    Resources resources = gContext.getResources(); 
    float scale = resources.getDisplayMetrics().density; 
    Bitmap bitmap = 
      BitmapFactory.decodeResource(resources, gResId); 

    android.graphics.Bitmap.Config bitmapConfig = 
      bitmap.getConfig(); 
    if(bitmapConfig == null) { 
     bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; 
    } 
    bitmap = bitmap.copy(bitmapConfig, true); 

    Canvas canvas = new Canvas(bitmap); 
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    paint.setColor(Color.BLACK); 
    paint.setTextSize((int) (15 * scale)); 
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE); 

    Rect bounds = new Rect(); 
    paint.getTextBounds(gText, 0, gText.length(), bounds); 
    int x = (bitmap.getWidth() - bounds.width())/2; 
    int y = (bitmap.getHeight() + bounds.height())/2; 

    canvas.drawText(gText, x * scale, y * scale, paint); 

    return bitmap; 
} 

я хочу, чтобы нарисовать текст как то

enter image description here

+1

вы просмотрели информационные окна? Https: //developers.google.com/maps/documentation/android/marker – Raghunandan

+0

@ Raghunandan да, я посмотрел. info окно показывает текст над маркером, но я хочу показать текст внутри маркера. –

+0

проверьте этот https://github.com/googlemaps/android-maps-utils или этот или новый веб-сайт: http://googlemaps.github.io/android-maps-utils/ и этот http://www.youtube .com/watch? v = nb2X9IjjZpM – Raghunandan

ответ

7

1.Downlaod библиотеки из

github.com/googlemaps/android-maps-utils

2. Использовать эту ссылку

Using android-maps-utils with ADT

TextIconGenerator tc = new TextIconGenerator(this); 
    Bitmap bmp = tc.makeIcon("hello"); 

Затем установите растровый объект карты

.icon(BitmapDescriptorFactory.fromBitmap(bmp))); 

ФОТОГРАФИЮ

enter image description here

+0

Как я могу показать текст внутри маркера? Я не хочу делать пузырь, как вы сделали. –

+0

@NumanAhmad, что не так с тем, что вы пробовали. – Raghunandan

+0

я хочу, чтобы нарисовать текст, как этот [я хочу, чтобы нарисовать текст подобного] [1] [1]: http://i.stack.imgur.com/cZ2aJ.jpg –

4

Вы можете использовать этот пример, чтобы нарисовать текст маркеров, который Вы должны установить LatLong:

public Marker showTextOnMarker(final Context context, final GoogleMap map, 
     final LatLng location, final String text, final int padding, 
     final int fontSize) { 
    Marker marker = null; 

    if (context == null || map == null || location == null || text == null 
      || fontSize <= 0) { 
     return marker; 
    } 

    final TextView textView = new TextView(context); 
    textView.setText(text); 
    textView.setTextSize(fontSize); 

    final Paint paintText = textView.getPaint(); 

    final Rect boundsText = new Rect(); 
    paintText.getTextBounds(text, 0, textView.length(), boundsText); 
    paintText.setTextAlign(Align.CENTER); 

    final Bitmap.Config conf = Bitmap.Config.ARGB_8888; 
    final Bitmap bmpText = Bitmap.createBitmap(boundsText.width() + 2 
      * padding, boundsText.height() + 2 * padding, conf); 

    final Canvas canvasText = new Canvas(bmpText); 
    paintText.setColor(Color.BLACK); 

    canvasText.drawText(text, canvasText.getWidth()/2, 
      canvasText.getHeight() - padding - boundsText.bottom, paintText); 

    final MarkerOptions markerOptions = new MarkerOptions() 
      .position(location) 
      .icon(BitmapDescriptorFactory.fromBitmap(bmpText)) 
      .anchor(0.5f, 1); 

    marker = map.addMarker(markerOptions); 

    return marker; 
}