徒手多边形-沿Y轴位移(略向下)

时间:2018-09-04 08:49:38

标签: android google-maps android-layout

要求:我想在地图上绘制一个手绘多边形,因此我使用了如下所述的触摸事件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/map_frame_layout"
tools:context=".fragment.MapFragment">
<include layout="@layout/maps_main_content"/>
<FrameLayout
    android:id="@+id/frameLayoutMaps"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

布局maps_main_content包含一个用于绘制多边形的FrameLayout。

下面是我用来绘制多边形和处理触摸事件的代码。 我曾经使用布尔变量Is_MAP_Moveable来启用/禁用绘制模式。

private Polygon polygon;
private List<LatLng> val = new ArrayList<>();
Boolean Is_MAP_Moveable = false;
private FrameLayout mapLayout;
mapLayout = rootView.findViewById(R.id.frameLayoutForDrawing);

mapLayout.setOnTouchListener((v, event) -> {

        if (Is_MAP_Moveable) {

            float x = event.getX();
            float y = event.getY();

            int[] location = new int[2];

            v.getLocationInWindow(location);

            //return new Point((int) viewX, (int) viewY);

            float screenX = event.getX();
            float screenY = event.getY();
            float viewX = screenX - v.getLeft();
            float viewY = screenY - v.getTop();


            int x_co = Math.round(x);
            int y_co = Math.round(y);

            android.graphics.Point x_y_points = new android.graphics.Point((int) viewX, (int) viewY);//new android.graphics.Point(x_co, y_co);

            LatLng latLng = googleMaps.getProjection().fromScreenLocation(x_y_points);
            latitude = latLng.latitude;

            longitude = latLng.longitude;

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // finger touches the screen
                    val.add(new LatLng(latitude, longitude));
                    break;

                case MotionEvent.ACTION_MOVE:
                    // finger moves on the screen
                    val.add(new LatLng(latitude, longitude));
                    break;

                case MotionEvent.ACTION_UP:
                    // finger leaves the screen
                    MapFragment.this.Draw_Map();
                    break;
            }
        }
        return Is_MAP_Moveable;
    });

public void Draw_Map() {

    if (polygon != null)
        polygon.remove();

    val.add(val.get(0));//to make to it complete
    rectOptions = new PolygonOptions();
    rectOptions.addAll(val);
    rectOptions.strokeColor(Color.BLUE);
    rectOptions.fillColor(Color.TRANSPARENT);
    polygon = googleMaps.addPolygon(rectOptions);
    val.clear();

    Is_MAP_Moveable = false;

}

我可以看到多边形,但是它在Y轴上相当低。X轴似乎还可以。

0 个答案:

没有答案