Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android, add Marker from Bitmap on googlemap

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.

like image 330
EyeQ Tech Avatar asked Sep 24 '13 15:09

EyeQ Tech


People also ask

How do I add a marker to Google Maps Android?

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.

How do I start a custom image on Google Maps marker for mobile app?

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.


2 Answers

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.

like image 139
Sharj Avatar answered Oct 04 '22 10:10

Sharj


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));
like image 37
Arpit Patel Avatar answered Oct 04 '22 09:10

Arpit Patel