Android 谷歌地图,如何让每个 Marker InfoWindow 打开不同的 Activity? [英] Android Google Maps, how to make each Marker InfoWindow open different Activity?

查看:15
本文介绍了Android 谷歌地图,如何让每个 Marker InfoWindow 打开不同的 Activity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码段在谷歌地图上显示多个位置.我将这些坐标作为数组获取.在地图上显示标记后,我想在单击标记的信息窗口后进行活动.单击后,单个标记的每个信息窗口应具有不同的活动.我有 4 个标记,我想通过单击信息窗口访问 4 个不同的活动.我应该如何实现这一点.谢谢

I use following code segment for display several locations on google map. I get those coordinates as a array. After displaying markers on the map i want to go for a activity after clicking Infowindows of marker. Each infowindow of single marker should have different activity after clicking it. I have 4 markers and i want to access 4 different activities by clicking infowindow. How should i implement this. Thank you

我在地图上放置标记的代码

My code of put markers on map

     Marker a1 = googleMap.addMarker(new MarkerOptions().position(a)
                .title(arr[0])
                .snippet(arr[1])            );
       Marker b1 = googleMap.addMarker(new MarkerOptions().position(b)
                .title(arr[9])
                .snippet(arr[10])                 );
        Marker c1= googleMap.addMarker(new MarkerOptions().position(c)
                .title(arr[18])
                .snippet(arr[19]));
        Marker d1= googleMap.addMarker(new MarkerOptions().position(d)
                .title(arr[27])
                .snippet(arr[28])); 

推荐答案

使用一个HashMap来存储marker ID和它应该打开哪个Activity的对应标识.

Use a HashMap to store the marker ID and it's corresponding identification of which Activity it should open.

然后,使用OnInfoWindowClickListener获取用户点击信息窗口的事件,并使用HashMap来确定要打开哪个Activity.

Then, use a OnInfoWindowClickListener to get the event of a user clicking the info window, and use the HashMap to determine which Activity to open.

将 HashMap 声明为实例变量:

Declare the HashMap as an instance variable:

//Declare HashMap to store mapping of marker to Activity
HashMap<String, String> markerMap = new HashMap<String, String>();

然后,每次添加 Marker 时,在 HashMap 中创建一个条目:

Then, each time you add a Marker, make an entry in the HashMap:

        String id = null;

        Marker a1 = googleMap.addMarker(new MarkerOptions().position(a)
                .title(arr[0])
                .snippet(arr[1]));

        id = a1.getId();
        markerMap.put(id, "a1");

        Marker b1 = googleMap.addMarker(new MarkerOptions().position(b)
                .title(arr[9])
                .snippet(arr[10]));

        id = b1.getId();
        markerMap.put(id, "b1");

        Marker c1= googleMap.addMarker(new MarkerOptions().position(c)
                .title(arr[18])
                .snippet(arr[19]));

        id = c1.getId();
        markerMap.put(id, "c1");

        Marker d1= googleMap.addMarker(new MarkerOptions().position(d)
                .title(arr[27])
                .snippet(arr[28]));

        id = d1.getId();
        markerMap.put(id, "d1");
    }

然后定义信息窗口点击监听器:

And then define the info window click listener:

    googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {

            String m = markerMap.get(marker.getId());

            if (m.equals("a1")){
                Intent i = new Intent(MainActivity.this, ActivityA1.class);
                startActivity(i);
            }
            else if (m.equals("b1")){
                Intent i = new Intent(MainActivity.this, ActivityB1.class);
                startActivity(i);
            }
            else if (m.equals("c1")){
                Intent i = new Intent(MainActivity.this, ActivityC1.class);
                startActivity(i);
            }
            else if (m.equals("d1")){
                Intent i = new Intent(MainActivity.this, ActivityD1.class);
                startActivity(i);
            }
        }
    });

这篇关于Android 谷歌地图,如何让每个 Marker InfoWindow 打开不同的 Activity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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