android - 图钉内带有用户图像的自定义标记

标签 android marker

我正在尝试在 map 上显示用户,我已经在应用程序中实现了 map ,但现在我正在尝试制作自定义标记以在 pin 中显示用户的照片,如下所示: enter image description here

知道怎么做吗?

最佳答案

我通过引用 Telegram 应用程序来完成此操作 Telegram App

在谷歌地图中添加标记

GoogleMap mMap;
Marker marker;

LatLng latLng = new LatLng(Double.parseDouble(lat), Double.parseDouble(long));
MarkerOptions options = new MarkerOptions().position(latLng);
Bitmap bitmap = createUserBitmap();
if(bitmap!=null){
    options.title("Ketan Ramani");
    options.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
    options.anchor(0.5f, 0.907f);
    marker = mMap.addMarker(options);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
}

创建位图的函数

private Bitmap createUserBitmap() {
    Bitmap result = null;
    try {
        result = Bitmap.createBitmap(dp(62), dp(76), Bitmap.Config.ARGB_8888);
        result.eraseColor(Color.TRANSPARENT);
        Canvas canvas = new Canvas(result);
        Drawable drawable = getResources().getDrawable(R.drawable.livepin);
        drawable.setBounds(0, 0, dp(62), dp(76));
        drawable.draw(canvas);

        Paint roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        RectF bitmapRect = new RectF();
        canvas.save();

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.avatar);
        //Bitmap bitmap = BitmapFactory.decodeFile(path.toString()); /*generate bitmap here if your image comes from any url*/
        if (bitmap != null) {
            BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            Matrix matrix = new Matrix();
            float scale = dp(52) / (float) bitmap.getWidth();
            matrix.postTranslate(dp(5), dp(5));
            matrix.postScale(scale, scale);
            roundPaint.setShader(shader);
            shader.setLocalMatrix(matrix);
            bitmapRect.set(dp(5), dp(5), dp(52 + 5), dp(52 + 5));
            canvas.drawRoundRect(bitmapRect, dp(26), dp(26), roundPaint);
        }
        canvas.restore();
        try {
            canvas.setBitmap(null);
        } catch (Exception e) {}
    } catch (Throwable t) {
        t.printStackTrace();
    }
    return result;
}

根据设备密度计算dp

public int dp(float value) {
    if (value == 0) {
        return 0;
    }
    return (int) Math.ceil(getResources().getDisplayMetrics().density * value);
}

livepin.png

avatar.png

Output

关于android - 图钉内带有用户图像的自定义标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39315219/

相关文章:

android - 从标记中获取其他值

android - 根据 Google Maps V2 Android 上的用户方向旋转标记

java - 将 xml 字符串数组中的项目添加到 fragment 内的回收器 View 中

java - Android Studio 上 AndAR(r205) 示例代码 "nmatsui/AR_Speeker"的奇怪行为

android - 如何从谷歌地图中删除特定的多个标记而不是单个或所有标记

javascript - 显示带有数据信息的标记弹出窗口

javascript - 如何将点击功能添加到 gmap3 功能?

android - 迭代数组以更新屏幕

android - 避免使用多进程和应用程序 onCreate 崩溃

javascript - 具有 Web 服务调用的 Html5 应用程序可在模拟器上运行,但不能在设备上运行