【问题标题】:(Google Maps) Check if LatLng is within 80% of the visible region longitude(谷歌地图)检查 LatLng 是否在可见区域经度的 80% 以内
【发布时间】:2019-04-11 15:36:14
【问题描述】:

我有一个 android 布局,它有一个谷歌地图布局,底部覆盖着一个小的 LinearLayout。当用户选择地图上的标记时,这个小布局变得可见。现在,我想知道所选标记是否在选择时被小布局覆盖,比如它是否在地图经度可见区域的前 80% 范围内。我玩过maps.getProjection().getVisibleRegion(),但我不确定我从这里去哪里。提前感谢

【问题讨论】:

    标签: android google-maps android-layout google-maps-markers


    【解决方案1】:

    要检查 80%(或更多)的标记是否与您的小布局重叠,最好使用屏幕坐标,而不是 LatLng。您应该得到屏幕y 标记的坐标,标记heightheight 的“小布局”,而不是检查标记的y 坐标是否比小布局顶部的y 坐标大80%。

    所以用这样的东西:

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

    你得到了类似的东西:

    【讨论】:

    • 非常感谢。这才是真正帮助我的。虽然我做了一些调整,但这一次给了我一个想法。
    • @MetaSnarf 不客气!当然,这不是您任务的解决方案,只是方法。
    【解决方案2】:

    在确认地图已加载后尝试这样的操作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 2017-01-29
      • 2011-04-01
      • 1970-01-01
      • 2016-10-26
      • 2019-10-08
      相关资源
      最近更新 更多