Wit*_*cio 8 android google-maps marker google-maps-android-api-2
是否可以在 Google Maps API v2 中使用自定义视图而不是标记?遗憾的是,Google 不允许我们使用视图自定义标记(带有图像视图、文本视图等的相对布局),因此可以膨胀视图,然后将该视图放置到地图中的特定位置吗?
Yun*_*mre 10
您可以使用 IconGenerator 来实现此目的。
IconGenerator iconGenerator = new IconGenerator(context);
iconGenerator.setBackground(context.getDrawable(R.drawable.bg_custom_marker));
View inflatedView = View.inflate(context, R.layout.marker_custom, null);
iconGenerator.setContentView(inflatedView);
Marker marker = map.addMarker(
new MarkerOptions()
.position(new LatLng(-34.397, 150.644))
.icon(BitmapDescriptorFactory.fromBitmap(iconGenerator.makeIcon())));
Run Code Online (Sandbox Code Playgroud)
现在我们需要该marker_custom视图和bg_custom_marker可绘制对象。
标记_自定义.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@android:color/white"
tools:ignore="RtlHardcoded">
<TextView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm a custom view :)" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
bg_custom_marker.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#000000" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<corners android:radius="5dp" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
它看起来像下面这样:
编辑:iconGenerator.setBackground(null);可能更灵活。
好吧,你不能将视图放在 Google 地图上,但 @WitaloBenicio 说你可以将该视图转换为位图并将其显示在标记上
SO:将视图转换为位图
并使用
BitmapDescriptorFactory.fromBitmap (位图图像)添加到标记
mMap.addMarker(new MarkerOptions()..icon(BitmapDescriptorFactory.fromBitmap(bitmap)));
Run Code Online (Sandbox Code Playgroud)
我强烈喜欢去这个库
Google Maps Android API Utility Library并检查IconGenerator类Git Repo
| 归档时间: |
|
| 查看次数: |
7444 次 |
| 最近记录: |