Frage

Is there any way If I have like 100 geo points added in my MapActivity and I want to show only these which are like 1000m away from my current location. The other ones should not be visible on the map.

Any suggestions?

War es hilfreich?

Lösung 2

With some help of the first answer I find this solution :

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

and it worked.

Andere Tipps

Ok, here is the example. You can subclass Overlay class and override draw method. Whenever the Overlay is invalidated, you should check if your points are within 1000m from your location. Off course, you should know your location within your overlay class. The flowing is the example:

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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top