Search code examples
androidgoogle-mapsgeopoints

Android google maps show geo points which are closest to current location


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?


Solution

  • 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.