(Google Maps) Check if LatLng is within 80% of the visible region longitude

I have an android layout that have a google map layout overlayed with a small LinearLayout at the bottom. This small layout becomes visible when a marker on the map is selected by the user. Now, I wanna know if the selected marker gets overlayed by the small layout when selected, say if it is within first 80% of the visible region of the map longitude. I played around with the maps.getProjection().getVisibleRegion() but I'm not sure where I go from here. Thanks in advanceenter image description here

2 answers

  • answered 2018-11-08 09:30 Subrata Mondal

    Try something like this after you are confirmed that the map is loaded:

    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    
    LatLngBounds bounds = builder.build();
    LatLng ne = bounds.northeast;
    LatLng sw = bounds.southwest;
    
    Point neBoundInPx = mGoogleMap.getProjection().toScreenLocation(ne);
    Point swBoundInPx = mGoogleMap.getProjection().toScreenLocation(sw);
    
    Point newBottomBoundInPx = new Point (neBoundInPx.x , swBoundInPx.y - bottomLinearLayout.getHeight());
    
    LatLng newBottomBoundInPxLatLng = mGoogleMap.getProjection().fromScreenLocation(newBottomBoundInPx);
    
    if(placeLatLng.latitude <= ne.latitude &&
            placeLatLng.longitude >= sw.longitude &&
            placeLatLng.latitude >= newBottomBoundInPxLatLng.latitude &&
            placeLatLng.longitude <= ne.longitude) {
                //Yor place is above the linearLayout at the Bottom.
    }
    

  • answered 2018-11-08 09:50 Andrii Omelchenko

    For check if 80% (or more) of marker is overlapped by your small layout is better to use screen coordinates, not LatLng. You should get screen y coordinate of marker, marker height and height of "small layout", than check that the y coord of marker grater than y coord of top of small layout by 80%.

    So with something like this:

    public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
    
        private static final String TAG = MainActivity.class.getSimpleName();
    
        static final LatLng KYIV = new LatLng(50.450000, 30.523610);
    
        private GoogleMap mGoogleMap;
        private SupportMapFragment mMapSupportedFragment;
        private LinearLayout mSmallLinearLayout;
        private RelativeLayout mRootLayout;
        private Marker mMarker;
        private int mRootLayoutHeight;
        private int mSmallLayoutHeight;
        private int mMarkerWidth = 100;
        private int mMarkerHeight = 100;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mRootLayout = (RelativeLayout) findViewById(R.id.root_layout);
            mSmallLinearLayout = (LinearLayout) findViewById(R.id.small_bottom_layout);
    
            mMapSupportedFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
            mMapSupportedFragment.getMapAsync(MainActivity.this);
    
        }
    
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mGoogleMap = googleMap;
    
            mRootLayout.post(new Runnable() {
                @Override
                public void run() {
                    mRootLayoutHeight = mRootLayout.getHeight();
                }
            });
    
            mSmallLinearLayout.post(new Runnable() {
                @Override
                public void run() {
                    mSmallLayoutHeight = mSmallLinearLayout.getHeight();
                }
            });
    
            mMarker = mGoogleMap.addMarker(new MarkerOptions()
                    .position(KYIV)
                    .icon(getMarkerIcon(mMarkerWidth, mMarkerHeight)));
    
            mGoogleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
                @Override
                public void onCameraIdle() {
                    if (isMarkerOverlayed()) {
                        // do you magic here:
                        Toast.makeText(MainActivity.this, "Overlapped", Toast.LENGTH_SHORT).show();
                    };
                }
            });
    
        }
    
        private boolean isMarkerOverlayed() {
            boolean overlayed = false;
    
            LatLng markerLocation = mMarker.getPosition();
            Projection projection = mGoogleMap.getProjection();
            Point markerScreenPosition = projection.toScreenLocation(markerLocation);
    
            int smallLayoutTopY = mRootLayoutHeight - mSmallLayoutHeight;
    
            // 0.8 is 80% of marker height
            if (markerScreenPosition.y >= smallLayoutTopY + 0.8 * mMarkerHeight &&
                markerScreenPosition.y < mRootLayoutHeight) {
                overlayed = true;
            }
    
            return overlayed;
        }
    
        private BitmapDescriptor getMarkerIcon(int width, int height) {
    
            final Drawable iconDrawable = getResources().getDrawable(R.drawable.ic_marker);
            final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            iconDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            iconDrawable.draw(canvas);
    
            return BitmapDescriptorFactory.fromBitmap(bitmap);
        }
    
    }
    

    you got something like that:

    Marker overlay