将形状放入 Android 中的 MapView 中

发布于 2024-12-04 20:13:21 字数 178 浏览 1 评论 0原文

有没有办法在 MapView (纬度/经度区域)而不是像素区域中放置占据特定区域的形状(可绘制或任何类型的形状)。我需要它用于 GeoPoint 聚类目的,

如果不可能,任何使用投影坐标进行此操作的指导将不胜感激。但是使用 MapView 画布来执行此操作似乎并不明智,因为我一直在回收覆盖项目,并且我希望我也可以利用这一点。

Is there a way to place a shape (drawable or shape of any kind) that occupies a specific area in MapView (lat/lon area) not pixel area . I need that for GeoPoint Clustering purposes

If that is not possible any guidance to do it with projection coordinates would be greatly appreciated. But using the MapView canvas to do this doesn't seem performance-wise since i recycle my Overlay Items all the time and i wish i could take advantage of that too.

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

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

发布评论

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

评论(2

送你一个梦 2024-12-11 20:13:21

查看ItemizedOverlay

我正在从我的代码中剔除,所以这可能不会'开箱即用地编译,但应该给你足够的东西从这里弄清楚......

扩展类:

    public class MyOverlay extends ItemizedOverlay<OverlayItem>
    {
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;
    private OverlayItem item;

    public MyOverlay(Drawable defaultMarker, Context context) 
    {
          super(boundCenterBottom(defaultMarker));
          mContext = context;
    }

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

    }

    public void doPopulate()
    {
         populate();
    }




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

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

然后在你的活动中......

public void addLocations(GeoPoint _center)
{  
    final GeoPoint center = _center;

    mapOverlays = mapView.getOverlays();

    Drawable drawable = MyActivity.this.getResources().getDrawable(R.drawable.map_annotation_pin);

    itemizedoverlay = new ScoopOverlay(drawable,mContext);

//add as many points as you wish...
itemizedoverlay.addOverlay(
                    new OverlayItem(new GeoPoint(/*lon lat data here*/));
                );



showResults.sendEmptyMessage(0);
}

private Handler showResults = new Handler() 
{ 
    @Override 
    public void handleMessage(Message msg) 
    { 
        itemizedoverlay.doPopulate();
        mapOverlays.add(itemizedoverlay);

        mapView.invalidate();
    }
}; 

Look atItemizedOverlay

I'm culling from my code, so this probably wont' compile out of the box, but should give you enough to figure it out from here...

extended class:

    public class MyOverlay extends ItemizedOverlay<OverlayItem>
    {
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;
    private OverlayItem item;

    public MyOverlay(Drawable defaultMarker, Context context) 
    {
          super(boundCenterBottom(defaultMarker));
          mContext = context;
    }

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

    }

    public void doPopulate()
    {
         populate();
    }




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

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

and then in your activity....

public void addLocations(GeoPoint _center)
{  
    final GeoPoint center = _center;

    mapOverlays = mapView.getOverlays();

    Drawable drawable = MyActivity.this.getResources().getDrawable(R.drawable.map_annotation_pin);

    itemizedoverlay = new ScoopOverlay(drawable,mContext);

//add as many points as you wish...
itemizedoverlay.addOverlay(
                    new OverlayItem(new GeoPoint(/*lon lat data here*/));
                );



showResults.sendEmptyMessage(0);
}

private Handler showResults = new Handler() 
{ 
    @Override 
    public void handleMessage(Message msg) 
    { 
        itemizedoverlay.doPopulate();
        mapOverlays.add(itemizedoverlay);

        mapView.invalidate();
    }
}; 
酒解孤独 2024-12-11 20:13:21

因此,解决方案是绘制到保存项目的 ItemizedOverlay。

@Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        super.draw(canvas, mapView, shadow);

        // cycle through all overlays
        for (int index = 0; index < mOverlays.size(); index++) {
            OverlayItemExtended item = mOverlays.get(index);

            // Converts lat/lng-Point to coordinates on the screen
            GeoPoint point = item.getPoint();
            Point ptScreenCoord = new Point();
            mapView.getProjection().toPixels(point, ptScreenCoord);



                Paint boxPaint = new Paint();
                boxPaint.setColor(android.graphics.Color.WHITE);
                boxPaint.setStyle(Paint.Style.FILL);
                boxPaint.setAlpha(140);
                canvas.drawCircle(ptScreenCoord.X, ptScreenCoord.y,
                        20, boxPaint);


        }

}

So the solution is to draw to the ItemizedOverlay that holds the items.

@Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        super.draw(canvas, mapView, shadow);

        // cycle through all overlays
        for (int index = 0; index < mOverlays.size(); index++) {
            OverlayItemExtended item = mOverlays.get(index);

            // Converts lat/lng-Point to coordinates on the screen
            GeoPoint point = item.getPoint();
            Point ptScreenCoord = new Point();
            mapView.getProjection().toPixels(point, ptScreenCoord);



                Paint boxPaint = new Paint();
                boxPaint.setColor(android.graphics.Color.WHITE);
                boxPaint.setStyle(Paint.Style.FILL);
                boxPaint.setAlpha(140);
                canvas.drawCircle(ptScreenCoord.X, ptScreenCoord.y,
                        20, boxPaint);


        }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文