将自定义属性添加到标记(Google Map Android API V2) [英] Adding custom property to marker (Google Map Android API V2)

查看:167
本文介绍了将自定义属性添加到标记(Google Map Android API V2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Google Map API V3时,我可以通过以下方式添加自定义属性:

  var marker = new google.maps .Marker({
position:userLocation,
map:myGoogleMap,
animation:google.maps.Animation.DROP,
icon:avatar,
title:userName,
customProperty1:bla,
customProperty2:bla,
customProperty3:bla,
...

});

我想知道我是否可以对API V2 Android做同样的工作,我想这样做的原因每个标记的每个信息窗口都需要知道该标记的某些信息。我试图在下面的 render 函数中实现它:

  private void render(Marker marker,View view){
int badge = R.drawable.android_face;

if(marker.customProperty)
//这里我需要知道这个属性来决定使用哪个图片
badge = R.drawable.bla;

((ImageView)view.findViewById(R.id.badge))
.setImageResource(badge);



解决方案

直接扩展 Marker ,因为它是一个最终的类,但是你有一些选择:

0) 从Google Maps Android API v2 9.4.0开始,您可以使用 Marker :: getTag Marker :: setTag 。这可能是首选选项。



1)创建地图来存储所有附加信息:

 私人地图<标记,MyData> allMarkersMap = new HashMap< Marker,MyData>(); 

创建标记时,请将其添加到此地图中:

  Marker marker = map.addMarker(...); 
allMarkersMap.put(marker,myDataObj);

稍后在 render 函数中:

  MyData myDataObj = allMarkersMap.get(marker); 
if(myDataObj.customProp){...

2) strong>另一种方法是使用 Marker.snippet 将所有信息存储为 String ,稍后解析它,但这是一个有点丑陋,难以维护的解决方案。


$ b

3)从纯Google Maps Android API v2切换到 Android Maps Extensions



这与点非常相似1,但您可以使用

 标记将 MyData 直接存储到标记中.setData(myDataObj); 

以后:

  MyData myDataObj =(MyData)marker.getData(); 


When using Google Map API V3 I can add custom property in the following way:

var marker = new google.maps.Marker({
  position: userLocation,
  map: myGoogleMap,
  animation: google.maps.Animation.DROP,
  icon: avatar,
  title: userName,
  customProperty1: bla,
  customProperty2: bla,
  customProperty3: bla,
  ...

});

I'm wondering if I can do the same for API V2 Android, the reason I want to do this is that each info window of each marker need to know some information of that marker. And I'm trying to achieve this in render function below:

private void render(Marker marker, View view) {
        int badge = R.drawable.android_face;

        if (marker.customProperty)
        //here I need to know this property to decide which image to use        
                         badge = R.drawable.bla;

        ((ImageView) view.findViewById(R.id.badge))
                    .setImageResource(badge);

}

解决方案

You cannot directly extend Marker, because it is a final class, but you have some options:

0) As of Google Maps Android API v2 version 9.4.0, you can use Marker::getTag and Marker::setTag. This is most likely the preferred option.

1) Create a map to store all additional information:

private Map<Marker, MyData> allMarkersMap = new HashMap<Marker, MyData>();

When creating a marker, add it to this map with your data:

Marker marker = map.addMarker(...);
allMarkersMap.put(marker, myDataObj);

Later in your render function:

MyData myDataObj = allMarkersMap.get(marker);
if (myDataObj.customProp) { ...

2) Another way would be to use Marker.snippet to store all the info as a String and later parse it, but that's kinda ugly and unmaintainable solution.

3) Switch from plain Google Maps Android API v2 to Android Maps Extensions.

This is very similar to point 1, but you can directly store MyData into marker, using

marker.setData(myDataObj);

and later:

MyData myDataObj = (MyData) marker.getData();

这篇关于将自定义属性添加到标记(Google Map Android API V2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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