(Google Maps) 检查 LatLng 是否在可见区域经度的 80% 以内
(Google Maps) Check if LatLng is within 80% of the visible region longitude
我有一个 android 布局,其中 google 地图布局在底部覆盖了一个小 LinearLayout
。当用户选择地图上的标记时,这个小布局就会变得可见。现在,我想知道选中的标记是否在选中时被小布局覆盖,比如它是否在地图经度可见区域的前 80% 以内。我玩过 maps.getProjection().getVisibleRegion()
但我不确定我从这里去哪里。提前致谢
确认地图已加载后,尝试这样的操作:
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.
}
要检查 80%(或更多)的标记是否与您的小布局重叠,最好使用屏幕坐标,而不是 LatLng
。你应该得到标记的屏幕 y
坐标,"small layout" 的标记 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);
}
}
你得到了类似的东西:
我有一个 android 布局,其中 google 地图布局在底部覆盖了一个小 LinearLayout
。当用户选择地图上的标记时,这个小布局就会变得可见。现在,我想知道选中的标记是否在选中时被小布局覆盖,比如它是否在地图经度可见区域的前 80% 以内。我玩过 maps.getProjection().getVisibleRegion()
但我不确定我从这里去哪里。提前致谢
确认地图已加载后,尝试这样的操作:
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.
}
要检查 80%(或更多)的标记是否与您的小布局重叠,最好使用屏幕坐标,而不是 LatLng
。你应该得到标记的屏幕 y
坐标,"small layout" 的标记 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);
}
}
你得到了类似的东西: