什么是识别标志和获得与此相关的一个对象的典型方式? [英] What's a typical way to identify markers and get the Object associated with it?

查看:116
本文介绍了什么是识别标志和获得与此相关的一个对象的典型方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用在我的应用程序之一的地图。我想知道什么是共同的/高效的方式来获得,当它被窃听标记的关联的对象。

I'm using a map in one of my applications. I'd like to know what's a common/efficient way to get the associated Object of a marker when it is tapped.

我已经打算一天是这样的:

What I've been planning to day is something like this:

首先,我创建对象的ArrayList并使用由返回的标记mMap.addMarker(......)这些对象的一个​​HashMap; 作为指数

First, I create an ArrayList of my Objects and a HashMap of those Objects using the markers returned by mMap.addMarker(...); as the index.

ArrayList<MyObject> items = new ArrayList<MyObject>();
HashMap<Marker, MyObject> markersAndObjects = new HashMap<Marker, MyObject>();

现在,我重写 onMarkerClick()像这样

public boolean onMarkerClick(Marker clickedMarker) {
    // send the object returned by markersAndObjects.get(clickedMarker) for processing
    return false;
}

在这个有什么想法?

Any thoughts on this?

推荐答案

推荐的方式做到这一点是有标记ID和用户自定义数据的哈希值。如果活动被杀死并恢复标记对象可能会改变,但该ID将保持不变。你映射将如下所示:

The recommended way to do this is to have a Hash with marker ID and your custom data. The Marker object might change if the activity is killed and restored but the ID will remain the same. You map would look like:

HashMap<String, MyObject> markersAndObjects = new HashMap<String, MyObject>();

标记对象都有一个getId()方法来获取ID。

Marker objects have a getId() method to get the ID.

这篇关于什么是识别标志和获得与此相关的一个对象的典型方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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