谷歌地图:定制ItemizedOverlay和OverlayItem,正确的方式来显示不同​​的标记 [英] GoogleMaps: custom ItemizedOverlay and OverlayItem, the correct way to show different marker

查看:178
本文介绍了谷歌地图:定制ItemizedOverlay和OverlayItem,正确的方式来显示不同​​的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找长有关扩展ItemizedOverlay和OverlayItem正道明确的信息,但结果我得到不满足我呢。 在几句话,我需要在地图上显示不同种类涉及到多个位置标记;要显示的标记的类型是由位置本身的具体性质来确定。我还需要当选择一个标记,以显示一定的标记物,而另一个当标记是未选择的或未聚焦的。 根据我的理解,这是我写的:

I've been searching long for clear informations about the right way of extending ItemizedOverlay and OverlayItem, but the result I get does not satisfy me yet. In a few words, I need to show on a map different kind of markers related to a number of locations; the type of the marker to be displayed is determined by specific properties of the location itself. I will also need to display a certain marker when a marker is selected, and another one when the marker is unselected or unfocused. According to what I understand, this is what I wrote:

public class MyItemizedOverlay extends ItemizedOverlay<MyOverlayItem> {

ArrayList<MyOverlayItem> items;
//...

public MyItemizedOverlay(Drawable drawable, /*...*/) {
    super(boundCenterBottom(drawable));
    //...
    populate();
}

public void addOverlay(MyOverlayItem overlay) {
    return this.items.add(overlay);
    populate();

@Override 
protected MyOverlayItem createItem(int index) {
    return this.items.get(index);
}

@Override
public int size() {
    return this.items.size();
}}

    public class MyOverlayItem extends OverlayItem {

//...

public Drawable getMarker(int stateBitset) {
    Drawable drawable;
    try {
        if (stateBitset == 0) {
            if (this.property.Equals("ON")) {
                drawable = this.context.getResources().getDrawable(R.drawable.on);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                return drawable;
            } else if (this.property.Equals("OFF")) {
                drawable = this.context.getResources().getDrawable(R.drawable.off);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                return drawable;
            } else {
                drawable = this.context.getResources().getDrawable(R.drawable.generic);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                return drawable;
            }
        } else {
            //same thing as above, just with different drawables.
        }
    } catch (Exception e) {
        //...
    }

    return null;
}

MyItemizedOverlay创建并填充MyOverlayItem在主MapActivity。现在: 正确放置的唯一标记是默认的。即,那些具有作为抽拉所述一个传递到MyItemizedOverlay的构造,并且被设置与boundCenterBottom(其设置图像的中间底部点对点到地图中的指定点)。当在MyOverlayItem类的getMarker方法不返回null,而另一个标记时,显示图像错误地(例如,阴影不遵循图像!)。在getMarker方法,你必须定义一个边框的绘制(与drawable.setBounds);但我认为,通过这种方式,但是,你没有指定什么应该是一个必须放置在地图上的点的位置。

MyItemizedOverlay is created and populated of MyOverlayItem in the main MapActivity. Now: the only markers correctly placed are the default ones. That is, the ones which have as drawable the one passed to the constructor of MyItemizedOverlay, and is set with boundCenterBottom (which set the middle bottom point of the image to point to the specified point in the map). When the getMarker method in MyOverlayItem class does not return null, and another marker is used, the image is displayed wrongly (for example, the shadow does not follow the image!). In getMarker method, you must define a bounding box for the drawable (with drawable.setBounds); but I think that in this way, however, you do not specify what should be the point that has to be placed in the point of the map.

因此​​,该问题是:如何可以指定,用于从OverlayItem对象的getMarker方法将ItemizedOverlay返回一个标记可拉伸,将要使用的边界,以被放置在地图(如在MyItemizedOverlay为完成与boundCenterBottom默认绘制)?什么是做到这一点的最好方法是什么?

Thus, the question is: how can I specify, for a marker drawable returned from the getMarker method of an OverlayItem object to a ItemizedOverlay, the bounds to be used to be placed in the map (as done with boundCenterBottom in MyItemizedOverlay for the default drawable)? And what is the best way to do it?

推荐答案

好了,经过反复试验,似乎这样的:

Ok, after repeated tests it seems that this:

drawable.setBounds(-drawable.getIntrinsicWidth()/2, -drawable.getIntrinsicHeight(), drawable.getIntrinsicWidth() /2, 0);

放在OverlayItem的getMarker方法有以BER返回的绘制做这项工作(如boundBottomCenter确实在ItemizedOverlay)。现在所有工作正常,甚至非默认标记正确显示。

placed in the getMarker method of OverlayItem for the drawable that has to ber returned does the job (as boundBottomCenter does in ItemizedOverlay). Now all is working fine, and even the non-default markers are displayed correctly.

干杯!

这篇关于谷歌地图:定制ItemizedOverlay和OverlayItem,正确的方式来显示不同​​的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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