gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:40:16 27 4
gpt4 key购买 nike

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

最佳答案

要检查 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

关于android - (谷歌地图)检查 LatLng 是否在可见区域经度的 80% 以内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53202464/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com