【问题标题】:How to set image on marker Whereas image is stored in Firebase storage如何在标记上设置图像而图像存储在 Firebase 存储中
【发布时间】:2017-12-22 08:43:18
【问题描述】:

我想在标记上设置图像。图像存储在 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】:

经过大量研究,我找到了一种方法,但您需要 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
            }
        })

此时,您可以直接在标记中插入位图(资源),只需将上面的代码调整一下即可。但是如果你想使用带有任何其他东西的白色边框的占位符,请使用这段代码,而 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()
}

字体: How does one use glide to download an image into a bitmap?

【讨论】:

  • 嘿。位图在占位符中实际上非常小,我无法弄清楚如何将其完美地放入占位符中。你能告诉我代码中的高度和宽度是什么吗?如果有意义的话。谢谢
  • 嗨,伙计。我现在只使用 Flutter,所以没有简单的方法来测试和帮助你。但我认为你可以使用这一行来增加位图的大小: bitmapFinal= Bitmap.createBitmap(dp(62f), dp(76f), Bitmap.Config.ARGB_8888) //改变位图的大小弄乱 dp´ s。您可以在我提供的第二种方法中跟踪示例,因为我使用它来更改占位符的大小,这也是位图。希望它可以帮助你。请告诉我。
  • 嘿,你能看看我关于stackoverflow的问题吗? stackoverflow.com/questions/68131621/…
  • 我确实尝试过处理 dp,但个人资料照片似乎不合适。
猜你喜欢
  • 2020-12-04
  • 2019-05-06
  • 2022-06-10
  • 2019-06-10
  • 1970-01-01
  • 2017-08-10
  • 2018-03-19
  • 2019-07-24
  • 2019-01-31
相关资源
最近更新 更多