如何在Android中为不同类型的标记指定onMarkercli

如何在Android中为不同类型的标记指定onMarkercli

本文介绍了如何在Android中为不同类型的标记指定onMarkerclick()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在谷歌地图上,我出于不同目的放置不同颜色的标记,在这里我希望每个标记(例如:所有绿色标记)的onMarkerclick()具有不同的功能.如何为此设置条件.这是我的代码创建一组标记

On google map i am placing different colors of markers for different purposes, here i want different functionality for onMarkerclick() for each marker( ex:all green color markers).How to make condition for this.here is my code to create one set of markers

@Override
public void onMapLongClick(LatLng touchlocation) {
    // TODO Auto-generated method stub
    googleMap.addMarker(new MarkerOptions()
            .position(touchlocation)
            .title("Event")
            .icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}

这是另一种标记物

// adding marker
        user_marker = googleMap.addMarker(new MarkerOptions()
                .position(USER_LOCATION)
                .title("User")
                .snippet("User registered Location:" + loc)
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_ROSE)));

对于附近的地方,我正在使用其他类型的标记器

For nearby places i am using another type of marker

在这里,我如何实现onMarkerClick()

here how i implemented onMarkerClick()

@Override
public boolean onMarkerClick(Marker marker) {
    // TODO Auto-generated method stub
    // googleMap.clear();
    Toast.makeText(getApplicationContext(), "USER MARKER",
            Toast.LENGTH_LONG).show();
    showDialog(DIALOG_LOGIN);

    return true;
}

在这里,我希望每种类型的标记具有不同的功能,如何为此编写条件.

here i want different functionality for each type of marker ,how to write a condition for that.help me.

推荐答案

有两种方法可以做到这一点.

There are a couple of ways to do this.

如果您所有的Marker都有一个可以区分的值(例如标题),则可以执行以下操作:

If all your Markers have a value you can distinguish on (e.g. title), you may do this:

String title = marker.getTitle();
if ("User".equals(title)) {
    // show dialog
} else if ("Event".equals(title)) {
    // do thing for events
} else {
    // do thing for nearby places
}

代码段可用于此目的,但是如果您不想显示诸如"USER""EVENT""NEARBY_PLACE"常量之类的代码段,则必须创建信息窗口View.

Snippet can be used for this, but you have to take care of creating info window View if you don't want to show snippets like "USER", "EVENT" or "NEARBY_PLACE" constants.

一种更通用的方法是将所有Marker存储在Set中(如果要为每个Marker分配附加值,则存储Map):

A more general way could be storing all your Markers in Sets (or Maps if you want to assign additional value to every Marker):

private Set<Marker> events;
private Set<Marker> nearbyPlaces;

,然后检查是否单击了Marker在以下集合之一中:

and later checking if clicked Marker is in one of these sets:

if (user_marker.equals(marker)) {
    // show dialog
} else if (events.contains(marker)) {
    // do thing for events
} else if (nearbyPlaces.contains(marker)) {
    // do thing for nearby places
} else {
    // something is wrong, better call police (assuming there are only 3 kinds of markers)
}

最后但并非最不重要的是,您可以使用 Android Maps扩展删除难看的if语句

Last but not least, you can use Android Maps Extensions to remove ugly if statements.

为每种Marker类型创建一个OnMarkerClickListener,例如:

Create one OnMarkerClickListener for each Marker type, e.g.:

private OnMarkerClickListener userMarkerClickListener = new OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        // show dialog
        return true;
    }
};

private OnMarkerClickListener eventMarkerClickListener = new OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        // do thing for events
        return false;
    }
};

在创建给定类型的Marker时,分配点击侦听器:

When creating Marker of a given type, assign click listener:

user_marker.setData(userMarkerClickListener);

在全局OnMarkerClickListener中,该全局对象已添加到GoogleMap对象调用中:

In your global OnMarkerClickListener, which is added to GoogleMap object call:

@Override
public boolean onMarkerClick(Marker marker) {
    OnMarkerClickListener listener = (OnMarkerClickListener) marker.getData();
    return listener.onMarkerClick(marker);
}

如果只有3种Marker,我会选择第二种选择,因为它既简单又健壮.如果超过5个或将来可能更改,使用第三个选项创建的面向对象的代码将更易于维护恕我直言.

If you have only 3 kinds of Markers, I would go for the second option, because it is simple and robust. If more than 5 or likely to change in the future, object-oriented code created using third option will be easier to maintain IMHO.

这篇关于如何在Android中为不同类型的标记指定onMarkerclick()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:43