固定(缩放独立)矩形谷歌地图

时间:2017-11-08 10:30:18

标签: android google-maps shape offline

如何在地图上添加一个代表80%宽度和80%屏幕高度的矩形,如地图离线区域选择屏幕。我需要得到这个矩形的LatLngBound,这里的LinearLayout可能不是解决方案。

1 个答案:

答案 0 :(得分:1)

您可以按getProjection()方法获取LatLon像素坐标,并使用onDraw() custom view方法绘制所需内容。

所以,就像在thisNSimon回答中一样,只需在地图活动布局xml中添加FrameView所需的透明度的自定义视图(例如MapFragment):

<fragment
    android:id="@+id/map_fragment"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<[your_package].FrameView
    android:id="@+id/frame_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

其中[your_package].FrameView

public class FrameView extends View {

    private Paint mTransparentPaint;
    private Paint mBorderPaint;
    private Paint mSemiBlackPaint;
    private Path mPath = new Path();
    private GoogleMap mGoogleMap = null;
    private float x1, y1, x2, y2;

    public FrameView(Context context) {
        super(context);
        init();
    }

    public FrameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FrameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mTransparentPaint = new Paint();
        mTransparentPaint.setColor(Color.TRANSPARENT);
        mTransparentPaint.setStyle(Paint.Style.FILL);

        mBorderPaint = new Paint();
        mBorderPaint.setColor(Color.BLUE);
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setStrokeWidth(10);

        mSemiBlackPaint = new Paint();
        mSemiBlackPaint.setColor(Color.TRANSPARENT);
        mSemiBlackPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        x1 = 0.1f * canvas.getWidth();
        y1 = 0.1f * canvas.getHeight();
        x2 = 0.9f * canvas.getWidth();
        y2 = 0.8f * canvas.getHeight();

        mPath.reset();

        mPath.addRect(x1, y1, x2, y2, Path.Direction.CW);
        mPath.setFillType(Path.FillType.INVERSE_EVEN_ODD);

        canvas.drawRect(x1, y1, x2, y2, mTransparentPaint);
        canvas.drawRect(x1, y1, x2, y2, mBorderPaint);
        canvas.drawPath(mPath, mSemiBlackPaint);

        canvas.clipPath(mPath);
        canvas.drawColor(Color.parseColor("#83000000"));
    }

    public void setMap(GoogleMap googleMap) {
        mGoogleMap = googleMap;
    }

    public LatLng getTopLeft() {
        return point2LatLng(new Point((int)x1, (int)y1));
    }

    public LatLng getTopRight() {
        return point2LatLng(new Point((int)x2, (int)y1));
    }

    public LatLng getBottomLeft() {
        return point2LatLng(new Point((int)x1, (int)y2));
    }

    public LatLng getBottomRight() {
        return point2LatLng(new Point((int)x2, (int)y2));
    }

    public LatLng point2LatLng(Point point) {
        if (mGoogleMap != null) {
            Projection projection = mGoogleMap.getProjection();
            return projection.fromScreenLocation(point);
        } else {
            return null;
        }
    }

}

其中x1,x2,y1,y2 - “frame”矩形的坐标,以像素为单位。

您需要在FrameView中获取onCreate()对象:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mFrameView = (FrameView) findViewById(R.id.frame_view);
    mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map_fragment);
    mapFragment.getMapAsync(this);

    ...
}

并在GoogleMap中为mFrameView设置onMapReady()个对象:

@Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;
    mFrameView.setMap(mGoogleMap);
    ...
}

现在,您可以在需要时通过LatLonmFrameView.getTopLeft()等来获取mFrameView.getTopRight()个电话:

enter image description here

NB!这只是自定义组件的快速而肮脏的示例。

相关问题