自定义Google地图的InfoWindow样式

原来以为google地图的InfoWindow样式是不能改的,原来是API藏得太深,找了半天终于找出来了.

自定义InfoWindow样式,必须创建InfoWindowAdapter接口的具体实现,然后通过GoogleMap.setInfoWindowAdapter()加载自定义设计.
InfoWindowAdapter包含两个需要您实现的方法:getInfowWindow(Marker)和getInfoContent(Marker).API首先调用getInfoWindow(Marker),如果返回Null,接着调用getInfoContent(Marker).如果仍返回null,系统会使用默认信息窗口.

google地图默认的InfoWindow样式

image.png

默认实现方法

static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298);
Marker melbourne = mMap.addMarker(new MarkerOptions()
                          .position(MELBOURNE)
                          .title("Melbourne")
                          .snippet("Population: 4,137,400"));

UI设计的实际样式

marker.jpg

关键代码

mGoogleMap.setInfoWindowAdapter(DeviceLocationInfoWindowAdapter(LayoutInflater.from(this)))

DeviceLocationInfoWindowAdapter.java

public class DeviceLocationInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    private  final LayoutInflater mInflater;

    public DeviceLocationInfoWindowAdapter(LayoutInflater mInflater) {
        this.mInflater = mInflater;
    }
    @Override
    public View getInfoWindow(Marker marker) {
        return initialData(marker);
    }
    @Override
    public View getInfoContents(Marker marker) {
     return initialData(marker);
    }

    @NotNull
    private View initialData(Marker marker) {
        final View popup=mInflater.inflate(R.layout.info_window_device_location,null);
        LinearLayout ll_device=popup.findViewById(R.id.ll_device);
        ...
        return popup;
    }
}

info_window_device_location.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:background="@mipmap/ic_pop"
        android:layout_margin="24dp"
        android:orientation="vertical"
        android:paddingTop="12dp"
        android:paddingStart="12dp"
        android:visibility="visible"
        android:paddingEnd="12dp"
        android:paddingBottom="20dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:gravity="center_vertical"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tv_server_site_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="..."
                android:textColor="#333333"
                android:textSize="16sp"
                android:textStyle="bold" />
            <TextView
                android:gravity="end"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/dla_nav"
                android:textColor="#FF2800"
                android:textSize="16sp" />
        </LinearLayout>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="6dp"
            android:text="网点地址网点地址网点地址网点网点地址网点地址网点地址网点"
            android:textColor="#333333"
            android:textSize="14sp" />


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:gravity="center_vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@mipmap/ic_phone"
                android:drawablePadding="3dp"
                android:text="@string/ssa_service_department"
                android:textColor="#888888"
                android:textSize="13sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="4dp"
                android:text="08031313131"
                android:textColor="@color/important_red"
                android:textSize="13sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="6dp"
            android:gravity="center_vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@mipmap/ic_phone"
                android:drawablePadding="3dp"
                android:text="@string/ssa_market_department"
                android:textColor="#888888"
                android:textSize="13sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="4dp"
                android:text="08031313131"
                android:textColor="@color/important_red"
                android:textSize="13sp" />
        </LinearLayout>



    </LinearLayout>

    </LinearLayout>
</FrameLayout>

那么相关的信息(实体bean)是如何传递给InfoWindow的呢?
很简单,marker有个tag方法

在marker中传入

marker.tag = deviceListBean

在Adapter的initialData中接收

    @NotNull
    private View initialData(Marker marker) {
        final View popup=mInflater.inflate(R.layout.info_window_device_location,null);
            DeviceListBean bean= (DeviceListBean) marker.getTag();
            tv_digger_serial_number.setText(bean.getSerialno());
        return popup;
    }

注意:绘制的信息窗口不是实时视图。系统会在视图返回时将其渲染为图像(使用 View.draw(Canvas))。这意味着,地图上的信息窗口不会反映对视图所做的任何后续更改。如需稍后(例如,在加载图像后)更新信息窗口,请调用 showInfoWindow()。此外,信息窗口不会遵循任何常规视图的典型互动(例如触摸或手势事件)。不过,您可以监听整个信息窗口上的一般点击事件。
信息窗口并非实时视图,而是以图像形式渲染在地图上的视图。因此,您在该视图上设置的任何监听器都会被忽略,并且您也无法区分该视图不同部分上的点击事件。建议您不要在自定义信息窗口内放置按钮、复选框或文本输入等互动组件。
参考
https://developers.google.com/maps/documentation/android-sdk/infowindows

如有问题,请联系我的微信---> vvvpp0

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,392评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,258评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,417评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,992评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,930评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,199评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,652评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,327评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,463评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,382评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,432评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,118评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,704评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,787评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,999评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,476评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,057评论 2 341