Android的谷歌地图标记信息窗口活动 [英] Android google maps marker InfoWindow activities

查看:163
本文介绍了Android的谷歌地图标记信息窗口活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面对谷歌地图显示的几个位置code段。我得到这些坐标作为阵列。在地图上显示的标记后,我想点击标记的信息窗口后,走了活动。单个标记的每个信息窗口应该点击后都有不同的活动。我有4个指标,我想通过点击信息窗口访问4个不同的活动。我应该如何实现这一点。谢谢

看跌标记在地图上的我的code

 标记A1 = googleMap.addMarker(新MarkerOptions()位置(一)
                .title伪(ARR [0])
                是.snippet(改编[1]));
       标记B1 = googleMap.addMarker(新MarkerOptions()。位置(B)
                .title伪(改编[9])
                是.snippet(改编[10]));
        标记C1 = googleMap.addMarker(新MarkerOptions()。位置(C)
                .title伪(ARR [18])
                是.snippet(ARR [19]));
        标记D1 = googleMap.addMarker(新MarkerOptions()。位置(d)
                .title伪(ARR [27])
                是.snippet(ARR [28]));
 

解决方案

使用一个HashMap来存储标记ID和它对应的识别应该打开的活动的。

然后,使用 OnInfoWindowClickListener 来获取用户点击信息窗口的情况下,并使用HashMap来确定要打开的活动。

声明的HashMap作为一个实例变量:

  //声明HashMap来存储标记映射到活动
HashMap的<字符串,字符串> markerMap =新的HashMap<字符串,字符串>();
 

然后,每次添加一个标记时,请在HashMap中的条目:

 字符串ID = NULL;

        标记A1 = googleMap.addMarker(新MarkerOptions()位置(一)
                .title伪(ARR [0])
                是.snippet(改编[1]));

        的id = a1.getId();
        markerMap.put(ID,A1);

        标记B1 = googleMap.addMarker(新MarkerOptions()。位置(B)
                .title伪(改编[9])
                是.snippet(改编[10]));

        的id = b1.getId();
        markerMap.put(ID,B1);

        标记C1 = googleMap.addMarker(新MarkerOptions()。位置(C)
                .title伪(ARR [18])
                是.snippet(ARR [19]));

        的id = c1.getId();
        markerMap.put(ID,C1);

        标记D1 = googleMap.addMarker(新MarkerOptions()。位置(d)
                .title伪(ARR [27])
                是.snippet(ARR [28]));

        的id = d1.getId();
        markerMap.put(ID,D1);
    }
 

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

  googleMap.setOnInfoWindowClickListener(新GoogleMap.OnInfoWindowClickListener(){
        @覆盖
        公共无效onInfoWindowClick(标记标记){

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

            如果(m.equals(A1)){
                意图I =新的意图(MainActivity.this,ActivityA1.class);
                startActivity(ⅰ);
            }
            否则如果(m.equals(B1)){
                意图I =新的意图(MainActivity.this,ActivityB1.class);
                startActivity(ⅰ);
            }
            否则如果(m.equals(C1)){
                意图I =新的意图(MainActivity.this,ActivityC1.class);
                startActivity(ⅰ);
            }
            否则如果(m.equals(D1)){
                意图I =新的意图(MainActivity.this,ActivityD1.class);
                startActivity(ⅰ);
            }
        }
    });
 

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

解决方案

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

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.

Declare the HashMap as an instance variable:

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

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的谷歌地图标记信息窗口活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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