如何将对象传递给Google Maps Android API V2标记? [英] How to pass an object to Google Maps Android API V2 Marker?

查看:64
本文介绍了如何将对象传递给Google Maps Android API V2标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个API会返回一些JSON数据。使用 GSON 库将JSON对象转换为Java对象。



我有一个由对象组成的列表,每个对象都有特定的信息(id,name,long,lat,status,street等)。
我将Markers添加到地图并且工作正常......但是当用户单击其中一个标记(每个标记应显示不同的信息)。

因此,我需要将对象(或数据)传递给显示特定标记的完整信息的另一活动。你有什么建议?

解决方案

您可以创建 HashMap 来管理标记和

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

注意:数据

当你在地图上添加你的标记时,您还需要将它们添加到地图(包含相关信息)。例如,如果您的数据在列表中:

  for(数据数据:列表){
标记标记= mMap.addMarker(new MarkerOptions()
.position(new LatLng(data.lat,data.long));
hashMap.put(marker,data);
}

然后,假设您要使用与标记相关的信息,当您单击信息窗口时,可以执行类似这:

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

注意:数据应该实现

I have an API witch returns some JSON data. Using GSON library I convert JSON objects to Java objects.

I have a list filled by objects which each of them holds particular information (id, name, long, lat, status, street, etc). I add Markers to the map and it works fine... But I want to show additional information i.e. 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<Marker, Data> hashMap = new HashMap<Marker, Data>();

Note: Data is the class thet include all your information (id, name, long, lat, status, street, etc).

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.

这篇关于如何将对象传递给Google Maps Android API V2标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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