Skip to content Skip to sidebar Skip to footer

Create Custom Marker Icons On Runtime Using Android Xml

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 compl

Solution 1:

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"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content" ><ImageViewandroid: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

ViewviewMarker= ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.custom_marker_layout, null);
    ImageViewmyImage= (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.

publicstatic Bitmap createDrawableFromView(Context context, View view) {
    DisplayMetricsdisplayMetrics=newDisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay()
            .getMetrics(displayMetrics);
    view.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.layout(0, 0, displayMetrics.widthPixels,
            displayMetrics.heightPixels);
    view.buildDrawingCache();
    Bitmapbitmap= Bitmap.createBitmap(view.getMeasuredWidth(),
            view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvascanvas=newCanvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

Its working for me. Thanks!!!

Solution 2:

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

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

Bitmapmarker= 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):

intxOffset=20;
    intyOffset=50;
    intxPosition= xOffset;
    intyPosition= yOffset;
    intnumberOfPixels= image.getHeight()*image.getWidth();
    int[] pixels = newint[numberOfPixels];
    image.getPixels(pixels,0,0,0,0,image.getWidth(), image.getHeight());


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

    }

    MarkerOptionsmarkerOptions=newMarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(marker));

Post a Comment for "Create Custom Marker Icons On Runtime Using Android Xml"