【问题标题】:Remove a specific GeoPoint from ItemizedOverlay?从 ItemizedOverlay 中删除特定的 GeoPoint?
【发布时间】:2012-05-18 16:34:55
【问题描述】:

我有一张地图,上面有很多点添加到 ItemizedOverlay。

OverlayItem overlayItem = new OverlayItem(theGeoPoint, title, description);
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);

有没有办法从 itemizedOverlay 中删除特定点?

例如,假设我在不同的纬度/经度添加了很多点,我希望删除之前添加的纬度:32.3121212 和经度:33.1230912 的点。

我怎样才能删除那个点??

我真的需要这个,所以我希望有人能提供帮助。

谢谢。

完整的故事场景(如果您对如何解决这个问题有不同的想法): 将事件添加到从数据库中捕获的地图。现在,当从数据库中删除事件时,我希望同步地图并仅删除已删除的事件。 (请不要建议我重新下载除已删除的点之外的所有点,即使我已经想到了,但这不是我想做的选择。:p)

【问题讨论】:

    标签: android itemizedoverlay


    【解决方案1】:

    使用 GeoPoints Array 创建您的 MapOverlay 并覆盖绘图功能:

    public class MapOverlay extends Overlay 
    {
    
        private ArrayList<GeoPoints>points;
        ...
    
    
        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
        {
                super.draw(canvas, mapView, shadow);      
                int len = points.size();  
                if(len > 0)
                {
                    for(int i = 0; i < len; i++)
                    {
                       // do with your points whatever you want
                       // you connect them, draw a bitmap over them  and etc.
                       // for example:
                       Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.pointer);
                       mapView.getProjection().toPixels(points.get(i), screenPts);
                       canvas.drawBitmap(bmp, screenPts.x-bmp.getWidth()/2, screenPts.y - bmp.getHeight()/2, null);  
                    } 
                }
        }
    
        public void addPoint(GeoPoint p)
        {
           // add point to the display array
        }
    
        public void removePointByIndex(int i)
        {
           points.remove(i);
        }
    
        public void removePointByCordinate(Double lat, Double lng)
        {
            int index = -1;
            int len = points.size();  
            if(len > 0)
            {
                    for(int i = 0; i < len; i++)
                    {
                         if((int)(lat*1E6) == points.get(i).getLatitudeE6() && (int)(lng*1E6) == points.get(i).getLongitudeE6())
                         {
                              index = i;
                         }
                    } 
                }
    
                if(index != -1)
                {
                    points.remove(index);
                }
            }
        }
    
        public void removePoint(GeoPoint p)
        {
            int index = -1;
            int len = points.size();  
            if(len > 0)
            {
                    for(int i = 0; i < len; i++)
                    {
                         if(p == points.get(i))
                         {
                              index = i;
                         }
                    } 
                }
    
                if(index != -1)
                {
                    points.remove(index);
                }
            }
        }
    
    }
    

    (我没有测试以上课程)

    然后在您的 MapActivity 类中,您可以:

    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setClickable(true);
    MapOverlay mapOverlay = new MapOverlay();                       
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.add(mapOverlay);
    

    尝试谷歌一些谷歌地图教程,也许你会找到更多的解决方案。

    【讨论】:

    • 感谢您为回答我付出的巨大努力。你在那里给的很好的代码。我可以理解很多,但我看不到可以实际删除特定点的部分。方法 removePoint(GeoPoint p),下面应该有什么?这就是困扰我的地方。另外,在您提到 MapActivity 类的部分,这些命令不会清除 listOfOverlays 中的所有点吗?
    • 这个方法一开始只调用一次,我从我的代码中复制过来。之前不需要清除。
    • removePoint(GeoPoint p) 只是从数组中删除一个点,对吧?但是如果我发送坐标,它怎么知道我在说哪一点呢? (地理点)
    • 我改进了我的代码,但我不确定它是否可以工作,没有经过测试。
    • 我设法按照您给我的想法做了一些事情,但我遇到了一些错误。请看看我的另一个问题,看看你能不能帮忙:) stackoverflow.com/questions/10653938/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多