Как определить, отображается ли геопункт в видимой в данный момент области? - PullRequest
10 голосов
/ 12 февраля 2010

Скажем, у меня есть контроль карты в моем приложении для Android. Если я знаю, что ориентир существует на определенной широте и долготе, как я могу определить, отображается ли этот ориентир в данный момент на экране пользователя? Есть ли способ получения координат для верхнего левого и нижнего правого углов видимой области?

Ответы [ 4 ]

6 голосов
/ 12 февраля 2011

Нечто подобное поможет.

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

    }
5 голосов
/ 12 февраля 2010

Вы можете проецировать GeoPoint на Point и проверять, является ли оно прямоугольным (0, ширина экрана), на (0, высота экрана)

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

1 голос
/ 03 декабря 2012

Присоединение к подсказкам здесь , здесь и здесь :

Вот полный код моего 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));
    }
}
0 голосов
/ 21 сентября 2012

Хорошо, так как мне потребовалось некоторое время, чтобы найти простое рабочее решение, я опубликую его здесь для всех после меня;) В вашем собственном подклассе 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
...