java - 绘制 donut 谷歌地图Android

标签 java android google-maps geometry

我想在谷歌地图上绘制叠加层,其中除了某个点周围 1.5 公里半径之外的所有内容都被遮蔽了。 我尝试使用带有大量边框的圆圈来实现此目的,因此我将透明中心和覆盖颜色放在边框中以实现此目的,但它渲染不正常。

    map.addCircle(new CircleOptions()
            .center(london)
            .radius(500)
            .strokeWidth(100)
            .strokeColor(R.color.map_overlay_color)
            .fillColor(Color.RED)); // not transparent for showcase

/image/6NFfI.png

所以我决定使用孔来处理多边形。

    List<LatLng> points = new ArrayList<LatLng>();
    points.add(new LatLng(london.latitude-2, london.longitude-2));
    points.add(new LatLng(london.latitude-2, london.longitude+2));
    points.add(new LatLng(london.latitude + 2, london.longitude + 2));
    points.add(new LatLng(london.latitude + 2, london.longitude - 2));


    List<LatLng> hole = new ArrayList<LatLng>();
    for(int i = 0; i < 360; i += 1){
        LatLng coords = new LatLng(london.latitude + (radius * Math.cos(Math.toRadians(i))), london.longitude + (radius * Math.sin(Math.toRadians(i))));
        hole.add(coords);
        Log.d("HOLE", coords.toString());
    }


    map.addPolygon(new PolygonOptions()
            .addAll(points)
            .addHole(hole)
            .strokeWidth(0)
            .fillColor(R.color.map_overlay_color));

但是经度距离会根据中心位置而变化,所以我得到这样的结果。 /image/kSXAB.png 如果不是椭圆形那就完美了:)。 我在互联网上找到了这个(jsfiddle.net/doktormolle/NLHf9)JS示例,但我在java中找不到函数google.maps.geometry.spherical.computeOffset。

如有任何帮助,我们将不胜感激。

最佳答案

有一个SphericalUtil.computeOffset() Android 中的方法,该方法返回从指定标题中的原点移动一段距离后得到的 LatLng。

基于您发布的代码的示例代码:

        LatLng locationSF = new LatLng(37.7577, -122.4376);
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.7577, -122.4376), 12));

        List<LatLng> points = new ArrayList<LatLng>();
        points.add(new LatLng(locationSF.latitude-2, locationSF.longitude-2));
        points.add(new LatLng(locationSF.latitude-2, locationSF.longitude+2));
        points.add(new LatLng(locationSF.latitude+2, locationSF.longitude+2));
        points.add(new LatLng(locationSF.latitude+2, locationSF.longitude-2));


        List<LatLng> hole = new ArrayList<LatLng>();
        float p = 360/360;
        float d =0;
        for(int i=0; i < 360; ++i, d+=p){
            hole.add(SphericalUtil.computeOffset(locationSF, 5000, d));
        }

        mMap.addPolygon(new PolygonOptions()
                .addAll(points)
                .addHole(hole)
                .strokeWidth(0)
                .fillColor(Color.argb(150, 0, 0, 0))); 

enter image description here

关于java - 绘制 donut 谷歌地图Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32860169/

相关文章:

android - 初始化.rc : invalid service name

android - resources.ap_ 在编译我的 android 项目时不生成

android - 了解内核 dmesg 时间戳

javascript - 谷歌地图 : Uncaught ReferenceError: google is not defined

javascript - 信息窗口打开时禁用鼠标滚动 map 缩放,但允许滚动信息窗口中的内容

java - SLICK && LWJGL 闪烁问题

java - 如何获取流的最大索引值

javascript - Google 上的 URL 变量 'My Maps'

java - 整数数组的交替元素之和

java - 使用 Python 代码通过 Hadoop 流运行 Wordcount