简体   繁体   English

根据缩放级别在地图上缩放自定义标记

[英]Scale custom marker on map based on zoom level

I'm drawing a simple circle on my map by doing the following: 通过执行以下操作,我在地图上绘制了一个简单的圆:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    try {
        boolean success = mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(getContext(), R.raw.style_json));
        if (!success) {
            Log.e("MapsActivityRaw", "Style parsing failed.");
        }

    } catch (Resources.NotFoundException e) {
        Log.e("MapsActivityRaw", "Can't find style.", e);
    }

    final LatLng myLatlng = new LatLng(-33.999564, 18.516763);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(myLatlng));
    mMap.animateCamera( CameraUpdateFactory.zoomTo( 11.0f ) );

    mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
        public void onMapLoaded() {

            bar.setVisibility(View.GONE);
            drawCircle(myLatlng);

        }
    });

}

private void drawCircle(LatLng point){
    CircleOptions circleOptions = new CircleOptions();
    circleOptions.center(point);
    circleOptions.radius(500);
    circleOptions.strokeColor(Color.BLACK);
    circleOptions.fillColor(0x30ff0000);
    circleOptions.strokeWidth(2);
    mMap.addCircle(circleOptions);

}

This works perfectly, a circle is drawn to the map as expected. 效果很好,按预期在地图上画了一个圆。

预览图片

The problem is that when I zoom in and out the size of the circle increases/decreases. 问题是当我放大和缩小时,圆圈的大小会增加/减小。


My Question: 我的问题:

I'm looking for a way to keep the size of the circle the same when zooming in and out. 我正在寻找一种在放大和缩小时保持圆圈大小不变的方法。

Also.. 也..

I don't want to use marker clustering. 我不想使用标记聚类。

Essentially what you need to do is define what proportion of the screen for the circle radius to be. 本质上,您需要做的是定义屏幕的圆半径比例。

In this example, it is always 10% of the width. 在此示例中,它始终是宽度的10%。

Given this, then compute the distance between longitudes of the screen and divide that distance by the desired proportion (radius). 在此前提下,然后计算屏幕经度之间的距离,然后将该距离除以所需的比例(半径)。

I suspect you'll want different proportions depending on orientation. 我怀疑您会根据方向需要不同的比例。

This code snippet draws a circle whose radius is 10% of the width of the screen and then listens for camera movements (including zoom) to adjust the radius based on the new longitude bounds. 此代码段绘制了一个半径为屏幕宽度10%的圆,然后侦听相机移动(包括缩放)以根据新的经度边界调整半径。

Circle c;

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    mMap.getUiSettings().setZoomControlsEnabled(true);
    LatLng p = new LatLng(39.171755, -86.510632);

    LatLngBounds llb = mMap.getProjection().getVisibleRegion().latLngBounds;
    LatLng ne = llb.northeast;
    LatLng sw = llb.southwest;
    double d = SphericalUtil.computeDistanceBetween(ne,new LatLng(ne.latitude,sw.longitude));
    double r = (d/10);

    c = mMap.addCircle(new CircleOptions().center(p).radius(r).strokeColor(Color.BLUE));

    mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
        @Override
        public void onCameraMove() {
            LatLngBounds llb = mMap.getProjection().getVisibleRegion().latLngBounds;
            LatLng ne = llb.northeast;
            LatLng sw = llb.southwest;
            double d = SphericalUtil.computeDistanceBetween(ne,new LatLng(ne.latitude,sw.longitude));
            double r = (d/10);

            c.setRadius(r);
        }
    });


}

Also note that a circle is not a marker - title needs updating (may help searchers in the future.) 另请注意,圆圈不是标记-标题需要更新(将来可能会帮助搜索者。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM