簡體   English   中英

如何在Google地圖上繪制靜態目標圓?

[英]How can I draw a static target circle on Google Maps?

我一直在尋找在Google地圖上方繪制靜態半徑的圓的方法,而我遇到的所有答案都描述了與經緯度長的坐標相關的繪圖標記和圓。

我需要的是:

在此處輸入圖片說明

圓圈和標記漂浮在Google Maps片段上方 ,即: 平移和縮放時,它保持靜態 這是棘手的部分: 我希望能夠獲取地圖中的覆蓋區域以進行處理 (例如:緯度,中心標記的長和圓的半徑取決於地圖上的縮放級別)。

我該如何實現? 提前致謝。

您可以創建自定義View來繪制圓。 我的示例基於外部填充的繪制透明圓

在這里,您可以找到有關如何創建自定義視圖的教程。

在我的示例中,我正在創建一個自定義的RadarOverlayView ,其radius參數用於計算面積。

我的自定義視圖代碼:

public class RadarOverlayView extends LinearLayout {
    private Bitmap windowFrame;
    private float radius = 0f;
    private int centerX = 0;
    private int centerY = 0;

    public RadarOverlayView(Context context) {
        super(context);
    }

    public RadarOverlayView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs, R.styleable.RadarOverlayView, 0, 0);

        try {
            radius = a.getDimension(R.styleable.RadarOverlayView_radius, 0f);
        } finally {
            a.recycle();
        }
    }

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

        if (windowFrame == null) {
            createWindowFrame();
        }
        canvas.drawBitmap(windowFrame, 0, 0, null);
    }

    @Override
    public boolean isEnabled() {
        return false;
    }

    @Override
    public boolean isClickable() {
        return false;
    }

    protected void createWindowFrame() {
        windowFrame = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas osCanvas = new Canvas(windowFrame);

        centerX = getWidth() / 2;
        centerY = getHeight() / 2;

        if (radius > 0) {
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

            // Draw the circunference
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.RED);
            paint.setAlpha(200);
            paint.setStrokeWidth(5);
            osCanvas.drawCircle(centerX, centerY, radius, paint);

            // Draw the circle
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.RED);
            paint.setAlpha(100);
            osCanvas.drawCircle(centerX, centerY, radius, paint);

            // Draw the center icon
            paint.setAlpha(255);
            Bitmap centerBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            osCanvas.drawBitmap(centerBitmap, centerX - centerBitmap.getWidth() / 2,
                    centerY - centerBitmap.getHeight() / 2,
                    paint);
        }
    }

    @Override
    public boolean isInEditMode() {
        return true;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        windowFrame = null;
    }

    public float getRadius() {
        return radius;
    }

    public int getCenterX() {
        return centerX;
    }

    public int getCenterY() {
        return centerY;
    }
}

我的attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RadarOverlayView">
        <attr name="radius" format="dimension" />
    </declare-styleable>
</resources>

我的activity_maps.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    <fragment
        android:id="@+id/map"
        android:name="myPackage.MySupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MapsActivity"/>
    <myPackage.RadarOverlayView
        android:id="@+id/radar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        app:radius="150dp" />
</RelativeLayout>

我的活動:

public class MapsActivity extends FragmentActivity implements GoogleMap.OnCameraChangeListener {
    private GoogleMap mMap;
    private RadarOverlayView radarView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        radarView = (RadarOverlayView) findViewById(R.id.radar);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        mMap.getUiSettings().setAllGesturesEnabled(true);
        mMap.getUiSettings().setZoomControlsEnabled(true);

        mMap.setOnCameraChangeListener(this);
    }

    @Override
    public void onCameraChange(final CameraPosition cameraPosition) {
        // Compute the area of the circle each time the camera changes

        LatLng center = mMap.getProjection().fromScreenLocation(
                new Point(radarView.getCenterX(), radarView.getCenterY()));
        LatLng right = mMap.getProjection().fromScreenLocation(
                new Point(radarView.getCenterX() + Math.round(radarView.getRadius()),
                        radarView.getCenterY()));

        Location locationCenter = new Location("center");
        locationCenter.setLatitude(center.latitude);
        locationCenter.setLongitude(center.longitude);

        Location locationRight = new Location("right");
        locationRight.setLatitude(right.latitude);
        locationRight.setLongitude(right.longitude);

        double geoRadius = locationCenter.distanceTo(locationRight);
        double geoArea = Math.PI * Math.pow(geoRadius, 2);

        // Uncomment to inspect the difference between
        // RadarOverlayView circle and geographic circle:
        // mMap.clear();
        // Circle circle = mMap.addCircle(new CircleOptions()
        //        .center(cameraPosition.target)
        //        .radius(geoRadius)
        //        .strokeColor(Color.GREEN)
        //        .fillColor(Color.BLUE));

        Toast.makeText(this, "Area: " + geoArea, Toast.LENGTH_SHORT).show();
    }
}

結果看起來像這樣,並在每次相機更改時顯示一個Toast ,該Toast的區域被圓圈覆蓋:

在此處輸入圖片說明

局限性:

該示例在View繪制一個完美的圓,但是根據縮放級別,不能保證該圓在地理上是准確的。

您可以看到,在高縮放級別下,如果您取消對onCameraChange方法中的mMap.addCircle代碼進行注釋,則自定義視圖繪制的圓和地理上精確的圓(基於相機目標和半徑)之間會有巨大差異:

在此處輸入圖片說明

由地圖的投影(WGS84)引起的這種差異在高縮放級別時巨大,而在低縮放級別時減小:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM