在Google地图中使用视图而不是标记 [英] Use a view instead of Marker in Google Maps

查看:36
本文介绍了在Google地图中使用视图而不是标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google Maps API v2中是否可以使用自定义视图而不是Marker? 令人遗憾的是,谷歌不允许我们自定义我们的标记与视图(与图像视图、文本视图等的相对布局),所以有可能放大视图,然后将该视图放到地图中的特定位置?

推荐答案

您可以使用图标生成器来实现此目的。

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())));

现在我们需要marker_custom视图和bg_custom_marker可绘制视图。

mark_stom.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>

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>

如下所示:

编辑:iconGenerator.setBackground(null);可能更灵活。

这篇关于在Google地图中使用视图而不是标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆