Android地图标记引脚,带有来自网址的动态图片

时间:2015-11-22 03:09:06

标签: android android-maps-v2

我想创建地图别针,其中包含来自服务器网址的图片。我创建了pin的视图,使用picasso获取图像,然后使视图成为位图,将其用作图钉的图标。但是图像没有显示......我只能看到我放在视图中的边框。

我认为这是由于视图转换为Bitmap引起的,因为毕加索将图像异步,所以当它将其转换为位图时,图像还不在视图中。

关于如何实现这一目标的任何想法?

private View preparePinView () {


    View marker = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.places_pin_layout, null);
    ImageView image = marker.findViewById(R.id.image1);
    Picasso.with(getActivity()).load(image1url).fit().centerCrop().into(image1);

    return marker;
}

// Convert a view to bitmap
public static Bitmap createDrawableFromView(Context context, View view) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    return bitmap;
}

---在标记

中设置视图
View pinView = preparePinView();
Bitmap placePinBM = createDrawableFromView(getActivity(), pinView);

Marker pinMarker = map.addMarker(new MarkerOptions()
    .position(markerLatLng)
    .icon(BitmapDescriptorFactory.fromBitmap(placePinBM)));

2 个答案:

答案 0 :(得分:1)

您应该使用onSucess回调,如下所示:

private View preparePinView () {

    View marker = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.places_pin_layout, null);
    final ImageView image = marker.findViewById(R.id.image1);
    Picasso.with(getActivity()).load(image1url).fit().centerCrop().into(image1, new com.squareup.picasso.Callback(){
        @Override
        public void onSuccess() {
            Bitmap placePinBM = createDrawableFromView(context, image);

            Marker pinMarker = map.addMarker(new MarkerOptions()
                    .position(markerLatLng)
                    .icon(BitmapDescriptorFactory.fromBitmap(placePinBM)));
        }

        @Override
        public void onError() {

        }
    });

    return marker;
}

// Convert a view to bitmap
public static Bitmap createDrawableFromView(Context context, View view) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    return bitmap;
}

答案 1 :(得分:1)

您可以从onCreate传递Url加载自定义标记

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@mipmap/ic_marker">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profile_image"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="3.5dp"
        android:layout_marginTop="1.5dp"
        android:src="@mipmap/ic_app" />
</RelativeLayout>

view_custom_marker.xml

$collection->update(array('_id' =>  new MongoId($id)),array('$set' => $data));

图片样本 enter image description here