Android 如何在google地图标记上附加TextView
我可能已经问了一个类似的问题,但没有得到任何答案,因为我对自己想做的事情并不十分明确。 因此,基本上我使用Chris Broadfoot的android maps utils()在我的android应用程序中的谷歌地图上的BubbleIcons中显示短信息 问题是,我想在地图上标记的相对位置上附加一个文本视图,以显示其他一些动态信息(与这个大型库生成的位图图片相反);对我来说是倒计时 有谁知道我如何通过编程来实现这一点Android 如何在google地图标记上附加TextView,android,google-maps,google-maps-markers,google-maps-api-2,Android,Google Maps,Google Maps Markers,Google Maps Api 2,我可能已经问了一个类似的问题,但没有得到任何答案,因为我对自己想做的事情并不十分明确。 因此,基本上我使用Chris Broadfoot的android maps utils()在我的android应用程序中的谷歌地图上的BubbleIcons中显示短信息 问题是,我想在地图上标记的相对位置上附加一个文本视图,以显示其他一些动态信息(与这个大型库生成的位图图片相反);对我来说是倒计时 有谁知道我如何通过编程来实现这一点 提前谢谢,祝你有愉快的一天 该库有一个类来处理标记图标,称为BubbleCo
提前谢谢,祝你有愉快的一天 该库有一个类来处理标记图标,称为BubbleConFactory。默认情况下,它会更改标记内的文本,但您可以通过如下设置内容视图将其更改为任何您想要的内容:
BubbleIconFactory mBubbleFactory = new BubbleIconFactory(this);
mBubbleFactory.setContentView(mImageView);
mImageView.setImageBitmap(.....);
Bitmap iconBitmap = mBubbleFactory.makeIcon();
mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(iconBitmap)).position(MyPosition).title("My current position"));
这就是为什么要在标记中使用图像,但它应该与所需的所有内容相同,因为在setContentView方法中将视图设置为参数。请注意,“地图”中的标记和信息窗口不是实时视图,但它们的渲染方式类似于图像,因此,如果不再次渲染,就无法“刷新”内容。视频将帮助您实现这一点,我不确定,但我想BubbleIconFactory现在可能会被弃用,您应该改用IconGenerator,在库代码中查找它。希望能有帮助
编辑:
还可以了解plains google maps的工作原理,以便更好地了解该库正在使用的内容:
为标记创建自定义布局,如下所示
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_marker_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:textSize="12sp"
android:gravity="center_horizontal"
android:textColor="@color/white"
android:background="@drawable/ic_center_pin_green_black"/>
</FrameLayout>
createDrawableFromView方法是
private Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.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;
}
非常感谢,我确实使用IconGenerator。我已经看过你链接的视频了,我在问自己应该把代码的哪一部分放在一个AssyncTask中,以便更快地加载图标生成。再次感谢。
private Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.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;
}