安卓,如何在Google地图上从位图中添加标记

7

我正在使用googlemap-android-api v2,并希望在运行时从位图创建标记。所以我做了以下操作:

        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)

似乎不起作用,我确定bitmapthumbnail都不为空。如果我使用.fromResource(R.drawable.some_image)而不是.fromBitmap,它就会显示。但是,正如我所说,我想在运行时根据用户的输入进行更改。
有什么提示吗?谢谢
更新:
如果我将其添加(即使用上面的代码)到托管地图的Activity / Fragment的onResume()中,则标记确实会显示。在用户浏览文件以获取filePath之后,在onActivityResult()中使用此代码之前。对我来说,这仍然很奇怪,因为onActivityResult()在UI线程上。无论如何,能工作就好。

嗨,你尝试过更改extractThumbnail中的值吗?10, 10太小了。 - HoodVinci
2个回答

9
我正在按照以下方式进行操作: 首先,我会在运行时从路径创建一个Drawable。
Drawable drawable = Drawable.createFromPath(filePath);

然后我只需将它添加到标记器中,像这样
mMap.addMarker(new MarkerOptions()
            .position(location)
            .title(title)
            .icon(BitmapDescriptorFactory.fromBitmap(((BitmapDrawable)drawable).getBitmap())));

我希望这可以帮到你。

谢谢分享。它仍然没有显示。LogCat 显示 ERROR/GooglePlayServicesUtil(5951): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included. 我的地图和其他标记(使用 fromResource 创建)都显示了,所以我不知道那个错误是什么意思。 - EyeQ Tech
文件路径是什么?它是指本地文件还是服务器上的文件? - Sharjeel
谢谢您的跟进,这是一个本地文件。实际上我已经解决了并更新了我的帖子。 - EyeQ Tech

3
如果您有一个标记对象,那么您可以像下面的方法一样使用它。
 Bitmap smallDot = Bitmap.createScaledBitmap(getBitmapFromVectorDrawable(getApplicationContext(),
                R.drawable.red_dot), 15, 15, false);

marker.setIcon(BitmapDescriptorFactory.fromBitmap(smallDot));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接