如何在可绘制的自定义标记中显示文本

Ren*_*osa 11 android google-maps google-maps-markers

我正在尝试创建一个自定义标记,显示的文本的数字等于下面的示例:

例:

在此输入图像描述

但是,当您运行应用程序时,不显示文本,只显示没有数字的红色椭圆.

我的守则

InicializeMap()##

 private void InicializeMap() 
{
    if (_googleMap == null) 
    {
        _googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapMapeamento)).getMap();

        LatLng pos = new LatLng(23.4555453556, 11.145315551);

        MapUtils mapUtils = new MapUtils(getApplicationContext());
        Bitmap bitmap = mapUtils.GetBitmapMarker();

        Marker marker = _googleMap.addMarker(new MarkerOptions()
                .position(pos)
                .icon(BitmapDescriptorFactory.fromBitmap(bitmap)));

        // check if map is created successfully or not
        if (_googleMap == null) 
            Toast.makeText(getApplicationContext(), Mensagens.erroCriarMapa, Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的方法来创建位图

public Bitmap GetBitmapMarker()
{
    Paint color = new Paint();
    color.setTextSize(35);
    color.setColor(Color.BLACK);

    int px = _mContext.getResources().getDimensionPixelSize(R.dimen.map_dot_marker_size);

    Bitmap mDotMarkerBitmap = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(mDotMarkerBitmap);
    canvas.drawText("Hello!", 30, 40, color);

    Drawable shape = _mContext.getResources().getDrawable(R.drawable.shape_marker_red);

    shape.setBounds(0, 0, mDotMarkerBitmap.getWidth(), mDotMarkerBitmap.getHeight());
    shape.draw(canvas);

    return mDotMarkerBitmap;
}
Run Code Online (Sandbox Code Playgroud)

RES /抽拉/ shape_marker_red

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <gradient
        android:angle="90"
        android:endColor="#f58383"
        android:startColor="#ee6464" />
    <stroke
        android:width="1dp"
        android:color="#a13939" />
</shape>
Run Code Online (Sandbox Code Playgroud)

 private void InicializeMap() 
{
    if (_googleMap == null) 
    {
        _googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapMapeamento)).getMap();

        LatLng pos = new LatLng(23.4555453556, 11.145315551);

        MapUtils mapUtils = new MapUtils(getApplicationContext());
        Bitmap bitmap = mapUtils.GetBitmapMarker();

        Marker marker = _googleMap.addMarker(new MarkerOptions()
                .position(pos)
                .icon(BitmapDescriptorFactory.fromBitmap(bitmap)));

        // check if map is created successfully or not
        if (_googleMap == null) 
            Toast.makeText(getApplicationContext(), Mensagens.erroCriarMapa, Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

Ren*_*osa 8

我得到了以下实现,并且它有效.实施情况如下:

MapActivity类

MapUtils mapUtils = new MapUtils(getApplicationContext());
        Bitmap bitmap = mapUtils.GetBitmapMarker(getApplicationContext(), R.drawable.marker_blue, "1");

        Marker marker = _googleMap.addMarker(new MarkerOptions()
                .position(pos)
                .icon(BitmapDescriptorFactory.fromBitmap(bitmap)));
Run Code Online (Sandbox Code Playgroud)

MapUtils类

public Bitmap GetBitmapMarker(Context mContext, int resourceId,  String mText) 
{
    try 
    {
        Resources resources = mContext.getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);

        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();

        // set default bitmap config if none
        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.WHITE);
        paint.setTextSize((int) (14 * scale));
        paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);

        // draw text to the Canvas center
        Rect bounds = new Rect();
        paint.getTextBounds(mText, 0, mText.length(), bounds);
        int x = (bitmap.getWidth() - bounds.width())/2;
        int y = (bitmap.getHeight() + bounds.height())/2;

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

        return bitmap;

    } 
    catch (Exception e) 
    {           
        return null;
    }
  }
Run Code Online (Sandbox Code Playgroud)


Kam*_*nde 6

这就是我提出的.

在此输入图像描述

首先,声明标记选项以在地图上添加标记.

MarkerOptions options = new MarkerOptions()
                        .position(new LatLng(GlobalApp.getInstance().getLoginUserInfo().getLatitude(),
                                GlobalApp.getInstance().getLoginUserInfo().getLongitude()))
                        .draggable(false)
                        .flat(false)
                        .icon(BitmapDescriptorFactory.fromBitmap(createStoreMarker()));
                Marker tempMarker = globalGoogleMap.addMarker(options);
Run Code Online (Sandbox Code Playgroud)

添加一个函数来扩展您的规范布局,您可以获得他们的参考并添加您想要的任何内容.这是魔术发生的地方.

private Bitmap createStoreMarker() {
        View markerLayout = getLayoutInflater().inflate(R.layout.store_marker_layout, null);

        ImageView markerImage = (ImageView) markerLayout.findViewById(R.id.marker_image);
        TextView markerRating = (TextView) markerLayout.findViewById(R.id.marker_text);
        markerImage.setImageResource(R.drawable.ic_home_marker);
        markerRating.setText(GlobalApp.getInstance().getLoginUserInfo().getStoreName());

        markerLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        markerLayout.layout(0, 0, markerLayout.getMeasuredWidth(), markerLayout.getMeasuredHeight());

        final Bitmap bitmap = Bitmap.createBitmap(markerLayout.getMeasuredWidth(), markerLayout.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        markerLayout.draw(canvas);
        return bitmap;
    }
Run Code Online (Sandbox Code Playgroud)

这是布局

store_marker_layout.xml我正在膨胀

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_store_marker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

    <TextView
        android:id="@+id/marker_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="8dp"
        android:text="dgdfgdfg"
        android:textColor="@android:color/black"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/marker_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_home_marker" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 我不知道为什么这不是正确的答案,就像一个魅力! (2认同)