(Google地图)检查LatLng是否在可见区域经度的80%以内

时间:2018-11-08 06:24:16

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

我有一个android布局,其google map布局在底部覆盖着一个小的LinearLayout。当用户在地图上选择标记时,这种小布局变得可见。现在,我想知道所选标记在选择时是否会被小布局覆盖,例如它是否位于地图经度可见区域的前80%之内。我和maps.getProjection().getVisibleRegion()一起玩,但是我不确定从这里去哪里。预先感谢enter image description here

2 个答案:

答案 0 :(得分:1)

在确认地图已加载后,尝试以下操作:

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

答案 1 :(得分:1)

要检查您的小版面是否重叠了80%(或更多)的标记,最好使用屏幕坐标,而不是LatLng。您应该获取标记的屏幕y,标记“ height”和“ height”的“小布局”坐标,然后检查标记的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);
    }

}

你有类似的东西:

Marker overlay