0

I am trying to create a custom marker in google maps.I want to add a custom layout as the marker

So i used the following code inside a webservice:

protected void onPostExecute(Void result) {
        View custom_layout = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout,null);
        ImageView iv_category_logo=(ImageView) custom_layout.findViewById(R.id.iv_category_logo);
        pinbit=Bitmap.createScaledBitmap(pinbit,75,56,false);
        iv_category_logo.setImageBitmap(pinbit);
        //pinbit=MainActivity.getCroppedBitmap(pinbit, 10);
        marker.icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(getApplicationContext(), custom_layout)));
      // adding marker


      googleMap.addMarker(marker);
    }

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;

But a classcastexception is shown in the following line:

((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

The logcat is shown below:

07-18 16:08:07.617: E/AndroidRuntime(22114): FATAL EXCEPTION: main
07-18 16:08:07.617: E/AndroidRuntime(22114): java.lang.ClassCastException: android.app.Application
07-18 16:08:07.617: E/AndroidRuntime(22114):    at com.igloo.storelocater.MainActivity.createDrawableFromView(MainActivity.java:610)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at com.igloo.storelocater.MainActivity$retrieveimage.onPostExecute(MainActivity.java:600)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at com.igloo.storelocater.MainActivity$retrieveimage.onPostExecute(MainActivity.java:1)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at android.os.AsyncTask.finish(AsyncTask.java:417)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at android.os.Looper.loop(Looper.java:130)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at android.app.ActivityThread.main(ActivityThread.java:3689)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at java.lang.reflect.Method.invokeNative(Native Method)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at java.lang.reflect.Method.invoke(Method.java:507)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-18 16:08:07.617: E/AndroidRuntime(22114):    at dalvik.system.NativeStart.main(Native Method)

}

please help!!

user3852672
  • 285
  • 1
  • 8
  • 17

1 Answers1

0

The problem is in using of getApplicationContext() method. It is strongly recommended to avoid using it when you're working with some screen local stuff. See documentation for this method. It does not return an Activity object. I would suggest you to use inter-process communication (IPC) to pass data between your Service and Activity. One possible way is here. For another ways you can read Developers page.

Community
  • 1
  • 1
Chaosit
  • 1,116
  • 7
  • 21