Using an xml Layout for a Google Map Marker for Android(为 Android 的 Google 地图标记使用 xml 布局)
问题描述
I want to use a layout and not a drawable for my marker
I am using with Google Maps Api 2 for an Android app.
Like this:
Marker m = googleMap
.addMarker(new MarkerOptions()
.position(LOC)
.snippet(user)
.title(name)
.icon(BitmapDescriptorFactory.fromResource(R.layout.map_marker)));
However, it seems this is only designed for use from drawable
folder.
I simply what an image with a background where I can change colors based on marker type. This is the layout I want to use:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#222"
android:id="@+id/llMarker">
<ImageView
android:id="@+id/ivMarker"
android:layout_marginTop="5dp"
android:layout_width="40dp"
android:layout_height="40dp" />
</LinearLayout>
Pretty basic. How can I use this as a marker? (or at least replicate the end result that I am going for?)
The documents say you can use this:
fromResource (int resourceId)
So I assumed a layout might work.
UPDATE:
I have taken the concept from answer below and reworked some things; this still is not working; no errors. Just will not show image.
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.map_marker, null);
m = googleMap
.addMarker(new MarkerOptions()
.position(LOC)
.snippet(user)
.title(name)
.icon(BitmapDescriptorFactory.fromBitmap(loadBitmapFromView(v))));
// method
public static Bitmap loadBitmapFromView(View v) {
if (v.getMeasuredHeight() <= 0) {
v.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
return b;
}
return null;
}
You cannot directly use a layout with BitmapDescriptorFactory.fromResource()
:
Creates a BitmapDescriptor using the resource id of an image.
However, you could draw the layout into a bitmap, and then use that. You'd need to:
- Inflate the layout from xml (with a
LayoutInflater
). - Change whatever properties you want (e.g. background color).
- Render this layout into a
Bitmap
, viaCanvas
, for example as described here. - Pass this bitmap into
BitmapDescriptorFactory.fromBitmap()
.
这篇关于为 Android 的 Google 地图标记使用 xml 布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为 Android 的 Google 地图标记使用 xml 布局
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01