【问题标题】:How can I determine if a geopoint is displayed in currently viewable area?如何确定地理点是否显示在当前可见区域中?
【发布时间】:2010-02-11 23:14:27
【问题描述】:

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

【问题讨论】:

标签: android android-mapview


【解决方案1】:

这样的事情就可以解决问题。

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

    }

【讨论】:

    【解决方案2】:

    您可以将 GeoPoint 投影到 Point 并检查它是否是框 (0, 屏幕宽度) by (0, 屏幕高度)

    见:https://developer.android.com/reference/com/google/android/gms/maps/Projection.html

    【解决方案3】:

    加入提示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));
        }
    }
    

    【讨论】:

      【解决方案4】:

      好的,因为我现在花了一些时间来找到一个简单的工作解决方案,我将在我之后将它发布在这里;) 在您自己的 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
      

      【讨论】:

      • 请停止将完全相同的答案重新发布到多个帖子。如果它们是重复的,请将它们标记为这样。如果不是,请根据所提出的具体问题调整您的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多