كيف يمكنني تحديد ما إذا كان يتم عرض Geopoint في المنطقة القابلة للعرض حاليا؟

StackOverflow https://stackoverflow.com/questions/2248510

  •  20-09-2019
  •  | 
  •  

سؤال

قل لدي عنصر تحكم mapeview في تطبيق Android الخاص بي. إذا كنت أعرف أناما موجودا بقيمة خطيرة وعرض خط الطول، فكيف يمكنني تحديد ما إذا كان هذا المعالم قابلا للعرض حاليا على شاشة المستخدم؟ هل هناك طريقة للحصول على الإحداثيات لأعلى الزوايا اليمنى اليسرى والسفلية للمنطقة المرئية؟

هل كانت مفيدة؟

المحلول

يمكنك مشروع Geopoint للإشارة والتحقق مما إذا كان المربع (0، عرض الشاشة) بواسطة (0، ارتفاع الشاشة)

يرى: https:/developer.android.com/Reference/com/Google/android/gms/maps/projection.html.

نصائح أخرى

شيء من هذا القبيل سوف تفعل الخدعة.

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

    }

الانضمام إلى النصائح هنا, هنا و هنا:

هنا هو رمز كامل الخاص بي 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));
    }
}

حسنا، استغرق الأمر بعض الوقت الآن للعثور على حل عمل بسيط وسأنشره هنا للجميع بعدي؛) في الفئة الفرعية الخاصة بك من 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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top