如何在谷歌地图中显示MarkerIcon和标题,就像Google Apps一样? [英] How to show both MarkerIcon and Title in google map as like Google Apps?
问题描述
当我在Google地图Android应用程序中找到最近的餐馆时,餐厅名称默认显示在标记图标附近。 (请参阅Google图片)。
但在我的应用程序中,当我搜索最近的餐馆时,我需要做同样的事情,我只能显示标记图标。 (请参阅我的申请图片)。
Google图片 :
我的应用图像
部分解决方案:
在这里我找到了部分解决方案,我们通过使用画布绘制文本来获得这一点。我使用以下代码引用这些链接
经过几天的研究,我用这个解决方案完成了。
我们可以将您自己的布局设置为IconGenerator
查看标记=((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)) .inflate(R.layout.custom_marker_layout,null);
TextView numTxt =(TextView)marker.findViewById(R.id.num_txt);
numTxt.setText(Naveen);
最终IconGenerator mIconGenerator = new IconGenerator(getApplicationContext());
mIconGenerator.setContentView(marker);
位图图标= mIconGenerator.makeIcon();
customMarker = mMap.addMarker(new MarkerOptions()
.position(markerLatLng)
.title(Title)
.snippet(Description)
。图标(BitmapDescriptorFactory.fromBitmap(icon))
.anchor(0.5f,1));
When I find nearest restaurants in Google's Maps Android Application the restaurant name is showing near to marker icon as default. (Refer Google Image).
But in my application I need to do same when I search for nearest restaurants, I able to display only marker icons. (Refer My Application Image).
Google Image:
My Application Image:
Partial Solution : Here I found partial solution for this we get this by drawing a text using canvas. I used below code refer by these links here and here But canvas drawing cutting of my text. Refer attached image TextDrawn Image
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(context.getResources(), drawableId)
.copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
paint.setTextAlign(Paint.Align.CENTER);
paint.setLinearText(true);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
paint.setTextSize(35);
Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);
Canvas canvas = new Canvas(bm);
//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, canvas.getHeight() + 2, canvas.getHeight() + 2, paint);
return bm;
}
After few days research I finishing with this solution. We can set your own layout to IconGenerator
View marker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout, null);
TextView numTxt = (TextView) marker.findViewById(R.id.num_txt);
numTxt.setText("Naveen");
final IconGenerator mIconGenerator = new IconGenerator(getApplicationContext());
mIconGenerator.setContentView(marker);
Bitmap icon = mIconGenerator.makeIcon();
customMarker = mMap.addMarker(new MarkerOptions()
.position(markerLatLng)
.title("Title")
.snippet("Description")
.icon(BitmapDescriptorFactory.fromBitmap(icon))
.anchor(0.5f, 1));
这篇关于如何在谷歌地图中显示MarkerIcon和标题,就像Google Apps一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!