Question

I am developing an android app in which I need to download an image from its URL, create a bitmap out of it and than display it on map as an icon. so far I have successfully completed to show bitmaps dynamically on map but I need to place my images on some background. something like this shown here How to create a custom-shaped bitmap marker with Android map API v2? any example or suggestion would be helpful. thanks...:)

Was it helpful?

Solution

I got the answer for my above problem I created a bitmap from view and then placed it on marker. my xml is below

    <?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" >
    <ImageView
    android:id="@+id/img_id"
    android:layout_width="@dimen/custom_profile_image"
    android:layout_height="@dimen/custom_profile_image"
    android:contentDescription="@string/content"
    android:scaleType="centerCrop" />
    </RelativeLayout>

inflating above xml

    View viewMarker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.custom_marker_layout, null);
    ImageView myImage = (ImageView) viewMarker.findViewById(R.id.img_id);

set Imagebitmap on imageview which you download from url on runtime

    myImage.setImageBitmap(Imagebitmap);

now pass view object to the method and return the bitmap out of it. like this

    Bitmap bmp=createDrawableFromView(context,viewMarker);

    googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude)).title(title)
            .snippet(snippet)
            .icon(BitmapDescriptorFactory.fromBitmap(bmp)));

now you can use returned bitmap anywhere, thats you'll be able to make custom marker icons using xmls.

    public static Bitmap createDrawableFromView(Context context, View view) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay()
            .getMetrics(displayMetrics);
    view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            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;
}

Its working for me. Thanks!!!

OTHER TIPS

There's probably a better solution, but maybe try this:

Put the background you need into your drawable folder and load the bitmap:

Bitmap marker = getResources().getDrawable(R.drawable.marker_background);

Edit a copy of it with the Image you downloaded (only pseudo, don't know if this works like this):

    int xOffset = 20;
    int yOffset = 50;
    int xPosition = xOffset;
    int yPosition = yOffset;
    int numberOfPixels = image.getHeight()*image.getWidth();
    int[] pixels = new int[numberOfPixels];
    image.getPixels(pixels,0,0,0,0,image.getWidth(), image.getHeight());


    for (int x = 0; x<numberOfPixels;x++){
        marker.setPixel(xPosition,yPosition,pixels[x]);
        if((xPosition-xOffset)%image.getWidth()==0){
            yPosition+=1;
            xPosition=xOffset;
        }else{
            xPosition++;
        }

    }

    MarkerOptions markerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(marker));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top