Rotating drawable via XML not working with marker on GoogleMap V2

▼魔方 西西 提交于 2019-12-12 10:09:49

问题


I would like to rotate a drawable with an xml-file, then show it on the map.

I do like this.

//...//
mo1.position(myLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.myxmldrawable));
map.addMarker(mo1);
//...//

XML file:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromDegrees="180"
        android:toDegrees="180"
        android:pivotX="50%"
        android:pivotY="50%"
        android:drawable="@drawable/pin"/>

If i ran the code the following exception caught:

07-16 13:10:18.347: W/System.err(26084): java.lang.NullPointerException
07-16 13:10:18.347: W/System.err(26084):    at maps.ah.bm.a(Unknown Source)
07-16 13:10:18.347: W/System.err(26084):    at maps.ah.bm.a(Unknown Source)
07-16 13:10:18.347: W/System.err(26084):    at maps.ah.an.a(Unknown Source)
07-16 13:10:18.347: W/System.err(26084):    at bgc.onTransact(SourceFile:167)
07-16 13:10:18.347: W/System.err(26084):    at android.os.Binder.transact(Binder.java:326)
07-16 13:10:18.347: W/System.err(26084):    at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
07-16 13:10:18.347: W/System.err(26084):    at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
07-16 13:10:18.347: W/System.err(26084):    at hu.illion.ayt.activities.MapAct$GetBolyakAsync.onPostExecute(MapAct.java:1177)
07-16 13:10:18.347: W/System.err(26084):    at hu.illion.ayt.activities.MapAct$GetBolyakAsync.onPostExecute(MapAct.java:1)
07-16 13:10:18.347: W/System.err(26084):    at android.os.AsyncTask.finish(AsyncTask.java:631)
07-16 13:10:18.347: W/System.err(26084):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-16 13:10:18.347: W/System.err(26084):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-16 13:10:18.357: W/System.err(26084):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 13:10:18.357: W/System.err(26084):    at android.os.Looper.loop(Looper.java:137)
07-16 13:10:18.357: W/System.err(26084):    at android.app.ActivityThread.main(ActivityThread.java:4867)
07-16 13:10:18.357: W/System.err(26084):    at java.lang.reflect.Method.invokeNative(Native Method)
07-16 13:10:18.357: W/System.err(26084):    at java.lang.reflect.Method.invoke(Method.java:511)
07-16 13:10:18.357: W/System.err(26084):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-16 13:10:18.357: W/System.err(26084):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-16 13:10:18.357: W/System.err(26084):    at dalvik.system.NativeStart.main(Native Method)

The excpetion points to the line where i would like to add the marker to the map.

Important: I have succeed with .png drawables. I have problem with xml defined drawable only (rotation...)

So what is this? GoogleMap cannot handle XML drawables or what?

E D I T :

Okay, so after i tried the suggestion, and saw it is not working, (still got nullptr exception) i just said what the heck i log out EVERYTHING.

And the results are very strange to me.

Logcat result:

 07-17 22:11:11.410: I/myResource(22971): myResource is: 2130837627
07-17 22:11:11.410: I/myBitmapDescriptor(22971): myBitmapDescriptor is: com.google.android.gms.maps.model.BitmapDescriptor@41b24230
07-17 22:11:11.410: I/myLatLng(22971): myLatLng is OK
07-17 22:11:11.410: I/myMarker(22971): myMarker is OK
07-17 22:11:11.410: I/myMarker(22971): myMarker is still OK
07-17 22:11:11.410: I/map(22971): map is OK

See? Everything has reference. None is NULL, yet the NullPointerException remains. And the code is:

MarkerOptions myMarker = new MarkerOptions();


                        LatLng myLatLng = new LatLng(Double.valueOf(myHashMap.get("lat2")), Double.valueOf(myHashMap.get("lng2")));

                        if (myHashMap.get("point_type").equals("1")) {

                            Bitmap icon = BitmapFactory.decodeResource(MapAct.this.getResources(), R.drawable.ellenfel);

                            mo1.position(kezdoLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.startbolya));
                            myMarker.position(myLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.zsuri0));
                        } else {
                            mo1.position(kezdoLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.endbolya));

                            int myResource = getResources().getIdentifier("zsuri180","drawable", getPackageName());

                            if(myResource==0)
                            {
                                Log.i("myResource", "myResource is null");
                            }
                            else
                            {
                                Log.i("myResource", "myResource is: "+myResource+"");
                            }

                            BitmapDescriptor myBitmapDescriptor = BitmapDescriptorFactory.fromResource(myResource);

                            if(myBitmapDescriptor==null)
                            {

                                Log.i("myBitmapDescriptor", "myBitmapDescriptor is null");
                            }
                            else
                            {
                                Log.i("myBitmapDescriptor", "myBitmapDescriptor is: "+myBitmapDescriptor);
                            }


                            if(myLatLng==null)
                            {
                                Log.i("myLatLng", "myLatLng is NULL");
                            }
                            else
                            {
                                Log.i("myLatLng", "myLatLng is OK");
                            }

                            myMarker.position(myLatLng);

                            if(myMarker==null)
                            {
                                Log.i("myMarker", "myMarker is NULL");
                            }
                            else
                            {
                                Log.i("myMarker", "myMarker is OK");
                            }


                            myMarker.icon(myBitmapDescriptor);


                            if(myMarker==null)
                            {
                                Log.i("myMarker", "myMarker is NULL");
                            }
                            else
                            {
                                Log.i("myMarker", "myMarker is still OK");
                            }

                            if(map==null)
                            {
                                Log.i("map", "map is NULL");
                            }
                            else
                            {
                                Log.i("map", "map is OK");
                            }


                        }

                        map.addMarker(myMarker);

回答1:


You cannot use non-PNG resources as a Marker's icon.

You can however load such Drawable into memory, draw it into a Bitmap using Canvas and send that Bitmap to MarkerOptions.icon using BitmapDescriptorFactory.fromBitmap(...).




回答2:


Ok the problem seems to me like that (as I thought):

You're having a problem in .icon(BitmapDescriptorFactory.fromResource(R.drawable.myxmldrawable), because R.drawable only pertains to the files inside the drawable folders.

To get the resource use this:

getResources().getIdentifier(myxmldrawable,"drawable", getPackageName()) The myxmldrawable used above is assigned variable for the XML icon attribute Use this to get the icon dynamically:

.icon(BitmapDescriptorFactory .fromResource(getResources().getIdentifier(icon,"drawable", getPackageName()))

That should do it for ya.

Cheers :)



来源:https://stackoverflow.com/questions/17675108/rotating-drawable-via-xml-not-working-with-marker-on-googlemap-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!