简体   繁体   English

如何将对象传递给Google Maps Android API V2 Marker?

[英]How to pass an object to Google Maps Android API V2 Marker?

I have an API witch returns some JSON data. 我有一个API返回一些JSON数据。 Using GSON library I convert JSON objects to Java objects. 使用GSON库我将JSON对象转换为Java对象。

I have a list filled by objects which each of them holds particular information (id, name, long, lat, status, street, etc). 我有一个由对象填充的列表,每个对象都包含特定信息(id,name,long,lat,status,street等)。 I add Markers to the map and it works fine... But I want to show additional information ie status, street and other info, when user clicks on one of the markers (each markers should display different information). 我将标记添加到地图中并且工作正常......但是当用户点击其中一个标记 (每个标记应显示不同的信息)时,我想显示其他信息,即状态,街道和其他信息。

So, I need to pass an object (or data) to another activity which will show whole information of particular marker. 所以,我需要将一个对象(或数据)传递给另一个活动,该活动将显示特定标记的完整信息。 What do you suggest? 你有什么建议?

You can create an HashMap for managing the mapping between Markers and related information. 您可以创建一个HashMap来管理标记和相关信息之间的映射。

HashMap<Marker, Data> hashMap = new HashMap<Marker, Data>();

Note : Data is the class thet include all your information (id, name, long, lat, status, street, etc). 注意Data是包含所有信息(id,名称,长,纬度,状态,街道等)的类。

When you add your markers on the map, you also need to add them to the map (with related informations). 在地图上添加标记时,还需要将它们添加到地图中(包含相关信息)。 For example if you have your data in a list: 例如,如果您将数据放在列表中:

for(Data data : list){
    Marker marker = mMap.addMarker(new MarkerOptions()
                            .position(new LatLng(data.lat, data.long));
    hashMap.put(marker, data);  
}

Then, supposing you want use the information associated with the marker, when you click the info window, you can do something like this: 然后,假设您想要使用与标记关联的信息,当您单击信息窗口时,您可以执行以下操作:

mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Data data = hashMap.get(marker);
                if(data!=null){
                    Intent intent = new Intent(mContext, YourActivity.class);
                    intent.putExtra(YourActivity.EXTRA_MESSAGE, data);
                    mContext.startActivity(intent);
                }
            }
        });

Note : Data should implement Parcelable , or you can just pass the id, if the information retrieved from json are persisted, for example in a database, and so after the Activity receives the id, it can get all other information from database. 注意 :数据应该实现Parcelable ,或者你可以只传递id,如果从json检索的信息是持久的,例如在数据库中,那么在Activity收到id后,它可以从数据库中获取所有其他信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM