使用毕加索在地图标记上加载自定义图标是第一次不起作用



我正在从布局文件中创建自定义标记图像,并在该文件中使用图像。我是使用毕加索(Picasso(在图像视图上设置图像,该图像是第一次不起作用,但第二次工作,图像成功地加载了图像。我已经阅读了Piccaso拥有的弱参考,并尝试了在各种堆栈溢出帖子中给出的解决方案,但似乎没有用。分享我的代码,任何帮助将不胜感激。

//adding marker method
 protected Marker createMarker(double latitude, double longitude,String markerText,String imageUrl) {
        Log.e("Adding","Marker");
        LatLng marker = new LatLng(latitude, longitude);
        CameraPosition cameraPosition = new CameraPosition.Builder().target(marker).zoom(16).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
        return googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(latitude, longitude))
                .title(markerText)
                .snippet("Salesman Info").icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(markerText,imageUrl))));
    }

// making drawable from layout file
 private Bitmap getMarkerBitmapFromView(String markerText,String imageUrl) {
        View customMarkerView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker, null);
        final ImageView markerImageView = (ImageView) customMarkerView.findViewById(R.id.profile_image);
        TextView markerTextView = (TextView)customMarkerView.findViewById(R.id.marker_text);
        markerTextView.setText(markerText);

        final Target target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                markerImageView.setImageBitmap(bitmap);
                Log.e("Loading marker","Image loaded");
            }
            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
                Log.e("Loading marker","Image failed");
            }
            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
                Log.e("Loading marker","Image prepared");
            }
        };
        markerImageView.setTag(target);
        Picasso.with(getContext())
                .load(imageUrl)
                .into(target);

        customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
        customMarkerView.buildDrawingCache();
        Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
        Drawable drawable = customMarkerView.getBackground();
        if (drawable != null)
            drawable.draw(canvas);
        customMarkerView.draw(canvas);
        return returnedBitmap;
    }

i通过将Glide库与简单的目标选项使用。

Glide.with(getContext())
                .asBitmap()
                .load("imageurl")
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        Marker m =googleMap.addMarker(new MarkerOptions()
                                .position(new LatLng(latitude, longitude))
                                .title(markerText)
                                .snippet("").icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(markerText,null,mCustomMarkerView))));
                        m.setTag(info);
                        super.onLoadFailed(errorDrawable);
                    }
                    @Override
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                        Marker m =googleMap.addMarker(new MarkerOptions()
                                .position(new LatLng(latitude, longitude))
                                .title(markerText)
                                .snippet("").icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(markerText,resource,mCustomMarkerView))));
                        m.setTag(info);
                    }
                    }
                );

最新更新