【问题标题】:Google Maps custom Marker with Business Logo带有企业徽标的 Google 地图自定义标记
【发布时间】:2015-09-17 04:58:04
【问题描述】:

对于 Android 应用,我需要为我的谷歌地图活动自定义标记。标准选项对我没有帮助。使用可以为每个标记设置的商业徽标来实现正确图标的最佳方法是什么?

更新:

对不起,要么我不够清楚,要么我没看到。我在文档或您给我的提示中找不到很多有用的东西。现在我构建了一个默认标记:

我有很多个人资料图片或徽标,需要在运行时根据某些条件放置在标记内,例如:

【问题讨论】:

标签: android google-maps customization marker


【解决方案1】:

有一个documentation 关于在位置上添加自定义标记。

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)));

【讨论】:

    【解决方案2】:

    要制作自定义视图,您必须使用 MarkerDemoActivity 类来使用自定义标记。如果您使用的是 google map Api V2.0。

    和其他解决方案为标记制作自定义视图。 添加此代码以添加地图标记:

    Marker myLocMarker = map.addMarker(new MarkerOptions()
            .position(myLocation)
            .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.bluebox, "your text goes here"))));
    

    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) ;
    
    }
    

    有关更多信息,请参阅link.

    【讨论】:

    • 谢谢,但我找不到对我有帮助的功能。你能看看我的更新吗?
    【解决方案3】:

    所以这就是我首先制作两张照片的方式。如上所示的标记,另一个由以下函数标记(所有代码都在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;
    }    
    

    然后我将它们传递给以下函数

    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;
    }
    

    我知道这可能不是最好的方法,尤其是我不喜欢标记中圆圈位置的硬编码。但到目前为止还有效

    【讨论】:

      猜你喜欢
      • 2012-03-24
      • 2011-10-04
      • 2016-02-05
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 2016-09-20
      相关资源
      最近更新 更多