在android地图中自定义标记,将文本放在图像中

时间:2016-03-17 07:36:33

标签: android google-maps

我有一个要显示为标记的图像,在该图像中我必须更新地图移动时的时间

现在面临的问题是

  1. 我不知道如何在自定义图片中显示时间。

    BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.drawable.ic_remaining_time);
        googleMap.addMarker(new MarkerOptions()
            .position(cameraPosition.target)
            .title(cameraPosition.toString())
            .icon(descriptor)                       
    );
    
  2. 请有人帮助我

3 个答案:

答案 0 :(得分:3)

每次改变地图时都要画画,或者移动并设置如下图标

marker.setIcon(BitmapDescriptorFactory.fromBitmap(CommonUtils.writeOnDrawable(mContext,R.drawable.ic_location,"your text here").getBitmap()));

// Bitmap Drawable方法

public static BitmapDrawable writeOnDrawable(Context context, int drawableId, String text){

            Bitmap bm = BitmapFactory.decodeResource(context.getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);

            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.WHITE);
            paint.setTextSize(30);
            paint.setTextAlign(Paint.Align.CENTER);
            Canvas canvas = new Canvas(bm);

            // Change the position of text here
            canvas.drawText(text,bm.getWidth()/2 //x position
            , bm.getHeight()/2  // y position
            , paint);
            return new BitmapDrawable(context.getResources(),bm);
        }

您可以在第一次在Google地图对象中添加标记时获取标记

 Marker marker = googleMap.addMarker(new MarkerOptions().position(new LatLng(mLocation.getLatitude(), mLocation.getLongitude())).title(allJob.getBusiness().getName()).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location_icon)));

答案 1 :(得分:0)

我不建议在标记中重复更新图像内的文本,因为要实现这一点,您必须使用图像和文本创建位图,并将该位图作为图标标记并且不是免费的(就内存使用而言) 。

但是你可以尝试,无论如何...以这种方式创建位图描述符并放在图标内:

public static Bitmap createDrawableFromView(Context context, View view) { 
  DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); 
  view.setLayoutParams( new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT ) );
  view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
  view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
  view.buildDrawingCache(); 
  Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); 
  Canvas canvas = new Canvas(bitmap); view.draw(canvas); 

  return bitmap;
}

将您可以设置为图标的视图传递给该函数,也可以为xml充气。

答案 2 :(得分:0)

尝试这样的事情 -

BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getActivity().getResources(),R.drawable.ic_remaining_time));

googleMap.addMarker(new MarkerOptions()
                        .position(cameraPosition.target)
                        .title(cameraPosition.toString())
                        .icon(descriptor)

        );

要获得自定义标记,请在标记位置(例如下方 -

)充气您的自定义布局
 private Bitmap fillCustomizedMarker(int markerResource) {
        //Log.d(TAG, "fillMarkerWithText ++");
        View markerLayout;

            markerLayout = getActivity().getLayoutInflater().inflate(R.layout.custom_marker, null);

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

        ImageView markerImage = (ImageView) markerLayout.findViewById(R.id.marker_image);
        TextView timeText = (TextView) markerLayout.findViewById(R.id.timeText);
        markerImage.setImageResource(markerResource);
        markerCost.setText(time);

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

要更新此布局,我建议您在android中使用处理程序。它将有效地更新您的UI。