I'm using googlemap-android-api v2, and want to create the marker from a Bitmap at runtime. So I do:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
//create the thumbnail to use as marker
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bitmap,10,10);
MarkerOptions markerOptions = new MarkerOptions().position(currentLatLng).icon(BitmapDescriptorFactory.fromBitmap(thumbnail));
mMap.addMarker(markerOptions)
It never seems to work, I'm sure both bitmap
and thumbnail
are not null. If instead of .fromBitmap
, I use .fromResource(R.drawable.some_image)
then it shows. However, as I said, I want to change at run-time from user's input.
Any tip? thanks
Updated:
The marker does show if I add it (i.e, use the above code) at onResume()
of the Activity/Fragment that host the map. Before I use this code at onActivityResult()
, after the user browse a file to get the filePath
. To me, it's still strange since onActivityResult()
is on the UI thread. Anyway, whatever works.
For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.
You can start by looking at the Canvas and Drawables from the Android Developer page. Now you also want to download a picture from an URL. URL url = new URL(user_image_url); HttpURLConnection conn = (HttpURLConnection) url. openConnection(); conn.
I am doing the same this like following
First I create a Drawable from a path on runtime
Drawable drawable = Drawable.createFromPath(filePath);
Then I simply add it to marker like this
mMap.addMarker(new MarkerOptions()
.position(location)
.title(title)
.icon(BitmapDescriptorFactory.fromBitmap(((BitmapDrawable)drawable).getBitmap())));
I hope it helps.
If you have a marker object then you can use it like the below method.
Bitmap smallDot = Bitmap.createScaledBitmap(getBitmapFromVectorDrawable(getApplicationContext(),
R.drawable.red_dot), 15, 15, false);
marker.setIcon(BitmapDescriptorFactory.fromBitmap(smallDot));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With