谷歌地图标记信息窗口OnClick事件

时间:2014-08-04 00:44:44

标签: android google-maps marker

我是谷歌地图API V2和Android,并尝试为谷歌地图标记设置onClick事件。基本上我有两套标记:

巴士站标记

private void addBusStopMarker(){
    map.addMarker(new MarkerOptions().position(new LatLng(1.28213,103.81721)).title("10009 - Bt Merah Ctrl").icon(BitmapDescriptorFactory.fromResource(R.drawable.busstopicon)).snippet("Average Commuters: 9,940"));
    map.addMarker(new MarkerOptions().position(new LatLng(1.28294,103.82166)).title("10089 - Jln Bt Merah - B08").icon(BitmapDescriptorFactory.fromResource(R.drawable.busstopicon)).snippet("Average Commuters: 2,050"));
}

学校标记

    private void addSchoolMarker(){
    map.addMarker(new MarkerOptions().position(new LatLng(1.28515,103.81509)).title("Gan Eng Seng Primary School").icon(BitmapDescriptorFactory.fromResource(R.drawable.school)).snippet("Address: 100 RedHill Close, 158901"));
    map.addMarker(new MarkerOptions().position(new LatLng(1.28555,103.81361)).title("Bt Merah Secondary School").icon(BitmapDescriptorFactory.fromResource(R.drawable.school)).snippet("Address: 100 Henderson Road, 158901"));
}

我的标记信息窗口onClick事件:

private void initilizeMap() {
    if (map == null) {
        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                R.id.map)).getMap();}
         });

        //Bus stop info window onClick event
        map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
               Intent intent = new Intent(context ,PopulationCharts.class);
               String title = marker.getTitle();
               intent.putExtra("markertitle", title);
               startActivity(intent);
            }
        });

        // check if map is created successfully or not
        if (map == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

基本上,如果单击信息窗口,它将显示图表,但这仅适用于公交车站标记。但是,根据上面的代码,即使我的学校标记信息窗口点击,它仍然会显示图表。我想知道有没有办法为我的学校标记禁用信息窗口onclick事件?

提前致谢。

1 个答案:

答案 0 :(得分:2)

您可以尝试检查您正在与之互动的Marker是否包含" school"标题:

@Override
public void onInfoWindowClick(Marker marker) {
    Intent intent = new Intent(context ,PopulationCharts.class);
    String title = marker.getTitle();
    if(!title.contains("school")){ // if bus stop
         intent.putExtra("markertitle", title);
         startActivity(intent);
    }else{
         // whatever you need to do for schools
    }
}