如何在标记上设置图像,而图像是存储在Firebase存储空间中的。

问题描述 投票:1回答:1

我想在标记上设置图像。图像存储在firerebase存储区,我有图像的下载链接。现在,我如何将该图片设置在标记上?

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    reference = reference.child("Profile");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot ds:dataSnapshot.getChildren()){
                latitude = (Double) ds.child("Latitude").getValue();
                longitude = (Double) ds.child("Longitude").getValue();
                String name=(String) ds.child("Name").getValue();
                String imgUrl = (String) ds.child("imageURL").getValue();//Here is image Url of marker
                LatLng latLng = new LatLng(latitude,longitude);
                mMap.addMarker(new MarkerOptions().position(latLng).title(name));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));

            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
android google-maps-api-3 firebase-realtime-database android-mapview android-maps
1个回答
0
投票

经过太多的研究,我找到了一个方法,但你需要Glide来工作。这段代码是在Kotlin.一旦你在存储firebase中有了图片的url。调用Glide这种方式来转换位图中的url链接。多亏了Glide,这很容易。

       var bitmapFinal : Bitmap?

        Glide.with(this)
         .asBitmap()
         .load(link)
         .into(object : CustomTarget<Bitmap>(){
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {

                bitmapFinal = createUserBitmapFinal(resource)  //here we will insert the bitmap we got with the link in a placehold with white border.

                val mark1 = mMap.addMarker(MarkerOptions().position(latLng).title("Você está aqui").icon(BitmapDescriptorFactory.fromBitmap(bitmapFinal)))

                mark1.tag=0

                mMap.setOnMarkerClickListener (this@MapsActivity)

            }
            override fun onLoadCleared(placeholder: Drawable?) {
                // this is called when imageView is cleared on lifecycle call or for
                // some other reason.
                // if you are referencing the bitmap somewhere else too other than this imageView
                // clear it here as you can no longer have the bitmap
            }
        })

此时,你可以直接在marker中插入位图(资源),只需修改上面的代码。但如果你想使用一个带有白色边框的其他东西的占位符,就用这段代码,而bitmapInicial是你在上面的Glide中刚得到的位图。

private fun createUserBitmapFinal(bitmapInicial: Bitmap?): Bitmap? {
    var result: Bitmap? = null
    try {
        result = Bitmap.createBitmap(dp(62f), dp(76f), Bitmap.Config.ARGB_8888) //change the size of the placeholder 
        result.eraseColor(Color.TRANSPARENT)
        val canvas = Canvas(result)
        val drawable: Drawable = resources.getDrawable(R.drawable.placeholder) 
        drawable.setBounds(0, 0, dp(62f), dp(76f)) //change the size of the placeholder, but you need to maintain the same proportion of the first line
        drawable.draw(canvas)
        val roundPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        val bitmapRect = RectF()
        canvas.save()

        if (bitmapInicial != null) {
            val shader =
                BitmapShader(bitmapInicial, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
            val matrix = Matrix()
            val scale: Float = dp(104f) / bitmapInicial.width.toFloat()  //reduce or augment here change the size of the original bitmap inside the placehoder. But you need to adjust the line bitmapRect with the same proportion
            matrix.postTranslate(5f, 5f) 
            matrix.postScale(scale, scale)
            roundPaint.shader = shader
            shader.setLocalMatrix(matrix)
            bitmapRect[10f, 10f, 104f+10f]=104f+10f    //change here too to change the size
            canvas.drawRoundRect(bitmapRect, 56f, 56f, roundPaint)  
        }

        canvas.restore()
        try {
            canvas.setBitmap(null)
        } catch (e: java.lang.Exception) {
        }
    } catch (t: Throwable) {
        t.printStackTrace()
    }
    return result
}

fun dp(value: Float): Int {
    return if (value == 0f) {
        0
    } else Math.ceil(resources.displayMetrics.density * value.toDouble()).toInt()
}

fonts: 如何使用Glide将图片下载成位图?

© www.soinside.com 2019 - 2024. All rights reserved.