1

有什么办法吗?如果我在我的 MapActivity 中添加了 100 个地理点,我只想显示距离我当前位置 1000 米的这些点。其他的不应该在地图上可见。

有什么建议么?

4

2 回答 2

1

好的,这是示例。您可以继承 Overlay 类并覆盖 draw 方法。每当覆盖失效时,您应该检查您的点是否在您所在位置的 1000m 范围内。当然,您应该知道您在覆盖类中的位置。流动就是例子:

public class MyMapOverlay extends Overlay {

Bitmap bmp;
Context context;
public MyMapOverlay()
{
    bmp = BitmapFactory.decodeResource(MyApplication.getContext().getResources(), R.drawable.myplace);
}

@Override   
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
{
    super.draw(canvas, mapView, shadow);                   
    Point screenPts = new Point();
    for(int i = 0;i<points.size(); i++)
    {
        GeoPoint point = points.get(i);

        Location locationA = new Location("point " + String.valueOf(i));  

        locationA.setLatitude(point.getLatitudeE6());  
        locationA.setLongitude(point.getLongitudeE6());    

        float distance = myLocation.distanceTo(locationA);
        if(distance < 1000.0)
        {
            mapView.getProjection().toPixels(p, screenPts);
            canvas.drawBitmap(bmp, screenPts.x-bmp.getWidth()/2, screenPts.y-bmp.getHeight(), null);
        }
    }        
    return true;
}
于 2012-10-09T09:33:48.400 回答
0

在第一个答案的帮助下,我找到了这个解决方案:

                if(location != null){
                Location locA = new Location("gps");

                GeoPoint myLoc = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
                mapController.animateTo(myLoc);

                String sqlbars = "SELECT DISTINCT id, latitude, longitude, address, " +
                        " (SELECT category FROM bars_categories WHERE bar_id=bars.id) AS category " +
                        " FROM bars WHERE is_active=1";
                Cursor curBars = dbHelper.executeSQLQuery(sqlbars);
                if(curBars != null){

                    if(curBars.getCount() > 0){

                        for(curBars.move(0);curBars.moveToNext();curBars.isAfterLast()){

                            double latitude = curBars.getDouble(curBars.getColumnIndex("latitude"));            
                            double longitude = curBars.getDouble(curBars.getColumnIndex("longitude"));
                            final List<Overlay> mapOverlays = mapView.getOverlays();
                            Drawable drawable = SofiaLiveNearbyActivity.this.getResources().getDrawable(R.drawable.pin_map_icon);
                            int markerWidth = drawable.getIntrinsicWidth();
                            int markerHeight = drawable.getIntrinsicHeight();
                            drawable.setBounds(0, markerHeight, markerWidth, 0);
                            final HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, SofiaLiveNearbyActivity.this);

                            GeoPoint point = new GeoPoint((int)(latitude * 1E6),(int)(longitude *1E6));
                            OverlayItem overlayitem =  new OverlayItem(point, "Type: "+category+"\n----------------\nAddress: "+address, id);

                            locA.setLatitude(latitude);
                            locA.setLongitude(longitude);

                            float distance = location.distanceTo(locA);

                            if(distance < 1000.0){
                                itemizedoverlay.addOverlay(overlayitem);
                                mapOverlays.add(itemizedoverlay);
                            }
                        }   
                    }
                    curBars.close();
                }

它奏效了。

于 2013-01-05T12:31:17.747 回答