固定(独立于缩放)矩形 Google 地图
Fixed (zoom independent) Rectangle Google Map
如何在地图上添加一个代表屏幕 80% 宽度和 80% 高度的矩形,就像地图离线区域选择屏幕一样。我需要得到这个矩形的 LatLngBound,LinearLayout 可能不是这里的解决方案。
你可以通过getProjection()
method, and draw everything you want in onDraw()
method of custom view得到LatLon
个像素坐标。
因此,就像在 answer of NSimon 中一样,只需在地图 activity 布局 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" 矩形的坐标(以像素为单位)。
你需要在 onCreate()
中获取 FrameView
对象:
@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);
...
}
并在 onMapReady()
中为 mFrameView
设置 GoogleMap
对象:
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mFrameView.setMap(mGoogleMap);
...
}
现在您可以通过 mFrameView.getTopLeft()
、mFrameView.getTopRight()
等方式获得 LatLon
坐标。在需要时调用:
注意!这只是自定义组件的简单示例。
如何在地图上添加一个代表屏幕 80% 宽度和 80% 高度的矩形,就像地图离线区域选择屏幕一样。我需要得到这个矩形的 LatLngBound,LinearLayout 可能不是这里的解决方案。
你可以通过getProjection()
method, and draw everything you want in onDraw()
method of custom view得到LatLon
个像素坐标。
因此,就像在 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" 矩形的坐标(以像素为单位)。
你需要在 onCreate()
中获取 FrameView
对象:
@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);
...
}
并在 onMapReady()
中为 mFrameView
设置 GoogleMap
对象:
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mFrameView.setMap(mGoogleMap);
...
}
现在您可以通过 mFrameView.getTopLeft()
、mFrameView.getTopRight()
等方式获得 LatLon
坐标。在需要时调用:
注意!这只是自定义组件的简单示例。