Android Google Map Overlay Ontap - PullRequest
       19

Android Google Map Overlay Ontap

0 голосов
/ 13 февраля 2012

Я создал 3 класса. Каждый из них расширяет оверлей. И у каждого из них есть «сложный» переопределенный метод розыгрыша. И именно поэтому я решил переопределить Overlay вместо использования ItemizedOverlay. Теперь для определения того, какой оверлей был задействован, было бы проще использовать ItemizedOverlay, но я все еще использую «нормальный» оверлей. Как правильно определить, какие из моих оверлеев были использованы.

Я переопределил метод onTap в каждом (из трех) классов, расширяющих Overlay. В результате, независимо от того, где на карте я касаюсь, вызывается onTap всех трех классов.

Должен ли я рассчитывать на основе аргумента метода onTap GeoPoint и моего текущего местоположения, прикоснулся ли я к своему чертежу или нет? Как это сделать правильно? Изменить все на ItemizedOverlay? Если да, то как сделать сложные рисунки некоторых оверлеев?

10x Привет

1 Ответ

2 голосов
/ 14 февраля 2012

Я создал решение:

@Override
public boolean onTap(GeoPoint geoPoint, MapView mapView) {
    // Gets mapView projection to use it with calculations
    Projection projection = mapView.getProjection();
    Point tapPoint = new Point();
    // Projects touched point on map to point on screen relative to top left corner
    projection.toPixels(geoPoint, tapPoint);

    // Gets my current GeoPoint location
    GeoPoint myLocationGeoPoint = getLocationGeoPoint();

    // Image on the screen width and height
    int pinWidth = myPin.getWidth();
    int pinHeight = myPin.getHeight();

    // Variable that handles whether we have touched on image or not
    boolean touched = false;

    if (myLocationGeoPoint != null) {
        Point myPoint = new Point();

        // Projects my current point on map to point on screen relative to top left corner
        projection.toPixels(myLocationGeoPoint , myPoint);

        // Because image bottom line is align with GeoPoint and this GeoPoint is in the middle of image we have to do some calculations
        // absDelatX should be smaller that half of image width because as I already said image is horizontally center aligned
        int absDeltaX = Math.abs(myPoint.x - tapPoint.x);
        if (absDeltaX < (pinWidth / 2)) {
            // Because image is vertically bottom aligned with GeoPoint we have to be sure that we hace touched above GeoPoint (watch out: points are top-left corner based)
            int deltaY = myPoint.y - tapPoint.y;
            if (deltaY < pinHeight) {
                // And if we have touched above GeoPoint and belov image height we have win!
                touched = true;
            }
        }
    }

    if (touched) {
        Log.d(TAG, "We have been touched! Yeah!");
    }

    return true;
}
...