Android谷歌地图显示最接近当前位置的地理位置

时间:2012-10-09 08:28:06

标签: android google-maps geopoints

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

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

好的,这是一个例子。您可以继承Overlay类并覆盖draw方法。每当Overlay无效时,您应检查您的积分是否在距您所在地1000米范围内。当然,您应该知道您在叠加层中的位置。流动的是例子:

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;
}

答案 1 :(得分: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();
                }

并且有效。