android 如何确定当前可视区域中是否显示了一个地理位置点?

6qqygrtg  于 2023-01-11  发布在  Android
关注(0)|答案(5)|浏览(134)

假设我的Android应用程序中有mapview控件。如果我知道某个地标存在于某个纬度和经度,我如何确定该地标当前是否在用户屏幕上可见?是否有方法获得可见区域左上角和右下角的坐标?

rnmwe5a2

rnmwe5a21#

像这样的东西会起作用的。

private boolean isCurrentLocationVisible()
    {
        Rect currentMapBoundsRect = new Rect();
        Point currentDevicePosition = new Point();
        GeoPoint deviceLocation = new GeoPoint((int) (bestCurrentLocation.getLatitude() * 1000000.0), (int) (bestCurrentLocation.getLongitude() * 1000000.0));

        mapView.getProjection().toPixels(deviceLocation, currentDevicePosition);
        mapView.getDrawingRect(currentMapBoundsRect);

        return currentMapBoundsRect.contains(currentDevicePosition.x, currentDevicePosition.y);

    }
yruzcnhs

yruzcnhs2#

您可以将GeoPoint投影到Point,并通过(0,屏幕高度)检查它是否为box(0,屏幕宽度)
参见:https://developer.android.com/reference/com/google/android/gms/maps/Projection.html

e5nszbig

e5nszbig3#

连接尖端hereherehere
下面是我的CustomMapView的完整代码:

package net.alouw.alouwCheckin.ui.map;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Pair;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

/**
 * @author Felipe Micaroni Lalli (micaroni@gmail.com)
 */
public class CustomMapView extends MapView {  
    public CustomMapView(Context context, String s) {
        super(context, s);
    }

    public CustomMapView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public CustomMapView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }

    /**
     *
     * @return a Pair of two pairs with: top right corner (lat, lon), bottom left corner (lat, lon)
     */
    public Pair<Pair<Double, Double>, Pair<Double, Double>> getMapCorners() {
        GeoPoint center = getMapCenter();
        int latitudeSpan = getLatitudeSpan();
        int longtitudeSpan = getLongitudeSpan();

        double topRightLat = (center.getLatitudeE6() + (latitudeSpan / 2.0d)) / 1.0E6;
        double topRightLon = (center.getLongitudeE6() + (longtitudeSpan / 2.0d)) / 1.0E6;

        double bottomLeftLat = (center.getLatitudeE6() - (latitudeSpan / 2.0d)) / 1.0E6;
        double bottomLeftLon = (center.getLongitudeE6() - (longtitudeSpan / 2.0d)) / 1.0E6;

        return new Pair<Pair<Double, Double>, Pair<Double, Double>>(
                new Pair<Double, Double>(topRightLat, topRightLon),
                new Pair<Double, Double>(bottomLeftLat, bottomLeftLon));
    }
}
nzkunb0c

nzkunb0c4#

如果你使用GoogleMap

mMap.getProjection().getVisibleRegion();

就这么简单。

p8h8hvxi

p8h8hvxi5#

好吧,因为它花了我一些时间,现在找到一个简单的工作解决方案,我将张贴在这里为所有后,我;)在您自己的MapView子类中,只需使用以下代码:

GeoPoint topLeft = this.getProjection().fromPixels(0, 0);
GeoPoint bottomRight = this.getProjection().fromPixels(getWidth(),getHeight());

就这样,然后你可以通过

topLeft.getLatitudeE6() / 1E6
topLeft.getLongitudeE6() / 1E6

以及

bottomRight.getLatitudeE6() / 1E6
bottomRight.getLongitudeE6() / 1E6

相关问题