多个覆盖项目在 Android 的地图视图上不可见

发布于 2024-11-29 07:46:19 字数 2440 浏览 1 评论 0原文

这是我在活动中使用的代码,用于在地图视图上显示多个标记。 我使用了一个扩展 itemizedoverlays 的自定义覆盖类。以下是该类的代码:

public class view1CustomOverlays extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

    boolean isClickable = false;
    Context context = null;
    Drawable orangeMarker = null;

    public view1CustomOverlays(Drawable defaultMarker, Context c,
            boolean isActive, Drawable inActiveMarker) {
        super(boundCenterBottom(defaultMarker));
        context = c;
        isClickable = isActive;
        orangeMarker = inActiveMarker;
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

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

我在主 UI 活动中使用该类。以下是我如何使用此类向我的地图视图添加叠加层:

更新

private void view1LoadDataOnMap(String[] poleCord) {
    try {
        Drawable redFlag = getResources().getDrawable(R.drawable.red);
        Drawable greenFlag = getResources().getDrawable(R.drawable.green);
        Drawable orangeFlag = getResources().getDrawable(R.drawable.orange);
        int noOfPoles = poleCord.length / 4;
        List<Overlay> list = map.getOverlays();
        list.clear();
        view1CustomOverlays customOverlay = null;
        for (int i = 0; i < noOfPoles; i=i+4) {
            Float lat = Float.parseFloat(poleCord[i]);
            Float lng = Float.parseFloat(poleCord[i+1]);
            String poleNumber = poleCord[i+2];
            String ticketId = poleCord[i+3];
            customOverlay = new view1CustomOverlays(greenFlag, this, true,
                    orangeFlag);
            GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            OverlayItem overlayItem = new OverlayItem(p, poleNumber, null);
            customOverlay.addOverlay(overlayItem);
            Log.i("adding overlay",overlayItem.toString());
        }
        list.add(customOverlay);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (NotFoundException e) {
        e.printStackTrace();
    }

}

屏幕上仅显示一个叠加项。可能是什么问题。我已经检查过有几个覆盖项目被添加到我的类中,但是当我将类添加到我的地图视图时,只绘制了一个覆盖项目。

我需要在地图上添加总共 81 个标记。所以我制作了 81 个覆盖项并将它们添加到覆盖层并在最后添加一次覆盖层。

先感谢您。

here is the code that i am using in my Activity to show multiple markers on the mapview.
i have used a customoverlay class which extends itemizedoverlays. Here is the code for that class:

public class view1CustomOverlays extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

    boolean isClickable = false;
    Context context = null;
    Drawable orangeMarker = null;

    public view1CustomOverlays(Drawable defaultMarker, Context c,
            boolean isActive, Drawable inActiveMarker) {
        super(boundCenterBottom(defaultMarker));
        context = c;
        isClickable = isActive;
        orangeMarker = inActiveMarker;
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

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

i am using this class in the main UI activity. Here is how i am using this class to add overlays to my mapview:

UPDATE

private void view1LoadDataOnMap(String[] poleCord) {
    try {
        Drawable redFlag = getResources().getDrawable(R.drawable.red);
        Drawable greenFlag = getResources().getDrawable(R.drawable.green);
        Drawable orangeFlag = getResources().getDrawable(R.drawable.orange);
        int noOfPoles = poleCord.length / 4;
        List<Overlay> list = map.getOverlays();
        list.clear();
        view1CustomOverlays customOverlay = null;
        for (int i = 0; i < noOfPoles; i=i+4) {
            Float lat = Float.parseFloat(poleCord[i]);
            Float lng = Float.parseFloat(poleCord[i+1]);
            String poleNumber = poleCord[i+2];
            String ticketId = poleCord[i+3];
            customOverlay = new view1CustomOverlays(greenFlag, this, true,
                    orangeFlag);
            GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            OverlayItem overlayItem = new OverlayItem(p, poleNumber, null);
            customOverlay.addOverlay(overlayItem);
            Log.i("adding overlay",overlayItem.toString());
        }
        list.add(customOverlay);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (NotFoundException e) {
        e.printStackTrace();
    }

}

Only one overlayitem is showing on the screen. What could be the problem. i have checked that there are several overlayitems being added to my class but when i add to the class to my mapview only one overlayitem is being drawn.

There are in total 81 markers that i need to add on the map. So i am making 81 overlayitems and adding them to the overlay and adding the overlay once at the end.

thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2024-12-06 07:46:19

您缺少:

myMap.getOverLays().add(customOverlay);

private void view1LoadDataOnMap(String[] poleCord) {
    try {
        Drawable redFlag = getResources().getDrawable(R.drawable.red);
        Drawable greenFlag = getResources().getDrawable(R.drawable.green);
        Drawable orangeFlag = getResources().getDrawable(R.drawable.orange);
        int noOfPoles = poleCord.length / 4;
        List<Overlay> list = map.getOverlays();
        list.clear();
        view1CustomOverlays customOverlay = null;
        for (int i = 0; i < noOfPoles; i++) {
            Float lat = Float.parseFloat(poleCord[0]);
            Float lng = Float.parseFloat(poleCord[1]);
            String poleNumber = poleCord[2];
            String ticketId = poleCord[3];
            customOverlay = new view1CustomOverlays(greenFlag, this, true,
                    orangeFlag);
            GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            OverlayItem overlayItem = new OverlayItem(p, poleNumber, null);
            customOverlay.addOverlay(overlayItem);
            myMap.getOverLays().add(customOverlay);
            Log.i("adding overlay",overlayItem.toString());
        }
        list.add(customOverlay);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (NotFoundException e) {
        e.printStackTrace();
    }

}

You are missing:

myMap.getOverLays().add(customOverlay);

In

private void view1LoadDataOnMap(String[] poleCord) {
    try {
        Drawable redFlag = getResources().getDrawable(R.drawable.red);
        Drawable greenFlag = getResources().getDrawable(R.drawable.green);
        Drawable orangeFlag = getResources().getDrawable(R.drawable.orange);
        int noOfPoles = poleCord.length / 4;
        List<Overlay> list = map.getOverlays();
        list.clear();
        view1CustomOverlays customOverlay = null;
        for (int i = 0; i < noOfPoles; i++) {
            Float lat = Float.parseFloat(poleCord[0]);
            Float lng = Float.parseFloat(poleCord[1]);
            String poleNumber = poleCord[2];
            String ticketId = poleCord[3];
            customOverlay = new view1CustomOverlays(greenFlag, this, true,
                    orangeFlag);
            GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            OverlayItem overlayItem = new OverlayItem(p, poleNumber, null);
            customOverlay.addOverlay(overlayItem);
            myMap.getOverLays().add(customOverlay);
            Log.i("adding overlay",overlayItem.toString());
        }
        list.add(customOverlay);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (NotFoundException e) {
        e.printStackTrace();
    }

}
囚你心 2024-12-06 07:46:19

无论极数如何,您都会继续添加元素 0、1、2 和 3。您可能有多个标记,但它们都在同一个位置。你需要让它成为“i”的

函数

for (int i = 0; i < noOfPoles; i++) {
            Float lat = Float.parseFloat(poleCord[ i * 4]);
            Float lng = Float.parseFloat(poleCord[i * 4 + 1]);
            String poleNumber = poleCord[i * 4 + 2];
            String ticketId = poleCord[i * 4 + 3];
            ....
}

Irrespective of the noOfPoles, you keep on adding elements 0,1,2 and 3. You probably have multiple markers but they're all in the same place. You need to make it a function of 'i'

like

for (int i = 0; i < noOfPoles; i++) {
            Float lat = Float.parseFloat(poleCord[ i * 4]);
            Float lng = Float.parseFloat(poleCord[i * 4 + 1]);
            String poleNumber = poleCord[i * 4 + 2];
            String ticketId = poleCord[i * 4 + 3];
            ....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文