[英]Google Maps custom Marker with Business Logo
for an Android App I need a custom marker for my google maps activity. 对于Android应用程序,我需要为Google Maps活动添加自定义标记。 The standard option do not help me.
标准选项对我没有帮助。 what is the best way to achieve the right icon with a businesslogo that can be set for each marker?
用可以为每个标记设置的businesslogo来实现正确的图标的最佳方法是什么?
UPDATE: 更新:
Sorry, either I wasn't clear enough or I don't see it. 抱歉,或者我不够清楚或者看不到。 I can't find a lot helpful things in the docs or the hints you gave me.
我在文档或您给我的提示中找不到很多有用的东西。 Now I constructed a default marker:
现在,我构造了一个默认标记:
and I have lots of profile pics or logos wich need to be placed within the marker at runtime depending on certain conditions eg: 而且我有很多个人资料图片或徽标,根据特定条件,需要在运行时将其放置在标记中,例如:
There is a documentation about adding custom markers on a position. 有一个有关在位置上添加自定义标记的文档 。
private static final LatLng MELBOURNE = new LatLng(-37.813, 144.962);
private Marker melbourne = mMap.addMarker(new MarkerOptions()
.position(MELBOURNE)
.title("Melbourne")
.snippet("Population: 4,137,400")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));
for making custom view you have to use MarkerDemoActivity
class to use a custom marker.if you are using google map Api V2.0. 要制作自定义视图,您必须使用
MarkerDemoActivity
类来使用自定义标记。如果您使用的是Google Map Api V2.0。
and other solution to make custom view for marker. 以及为标记创建自定义视图的其他解决方案。 Add this code to add marker of map:
添加以下代码以添加地图标记:
Marker myLocMarker = map.addMarker(new MarkerOptions()
.position(myLocation)
.icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.bluebox, "your text goes here"))));
the writeTextOnDrawable() Method: writeTextOnDrawable()方法:
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) ;
}
So this is how i did it firt i constructed two pics. 因此,这就是我制作两张照片的方式。 the marker as shown above and the other one by the following function (all code is found soewhere on stackoverflow):
上面显示的标记,另一个通过以下功能标记(在stackoverflow的所有位置都找到了所有代码):
public static Bitmap getCircleBitmap(Bitmap bm) {
int sice = Math.min((bm.getWidth()), (bm.getHeight()));
Bitmap bitmap = ThumbnailUtils.extractThumbnail(bm, sice, sice);
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffff0000;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth((float) 4);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
Then I pass them to the following function 然后我将它们传递给以下函数
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, 5, 5, null);
return bmOverlay;
}
I know its probably not the best way of doing it, especially i dont like the hard coding of the position of the circle in the marker. 我知道这样做可能不是最好的方法,尤其是我不喜欢标记中圆圈位置的硬编码。 but t works so far
但是到目前为止
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.