简体   繁体   English

绘制甜甜圈Google Maps Android

[英]Draw donut google maps Android

I want to draw overlay on google maps where everything except radius of 1.5km around certain point is shadowed out. 我想在Google地图上绘制叠加层,其中除特定点周围1.5公里半径以外的所有区域都被阴影掉了。 I tried to use circle with huge amount of border for this, so i would put transparent center and overlay color in border to achive this, but it doesn't render OK. 我试图为此使用带有大量边框的圆,所以我会在边框中放置透明的中心和覆盖颜色以达到此目的,但是这样无法呈现正常效果。

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

http://i.stack.imgur.com/6NFfI.png http://i.stack.imgur.com/6NFfI.png

So i decided to do it with polygon using hole. 因此,我决定使用孔对多边形进行处理。

    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));

But longtitude distance varies, depending on center position, so i get something like this. 但是经度距离会有所不同,具体取决于中心位置,因此我得到了类似的信息。 http://i.stack.imgur.com/kSXAB.png Which would be perfect if it wasn't oval :). http://i.stack.imgur.com/kSXAB.png如果不是椭圆形,那将是完美的选择。 I found out this (jsfiddle.net/doktormolle/NLHf9) JS example on the internet, but i can't find function google.maps.geometry.spherical.computeOffset in java. 我在互联网上找到了这个(jsfiddle.net/doktormolle/NLHf9)JS示例,但是我在Java中找不到函数google.maps.geometry.spherical.computeOffset。

Any help would be appreciated. 任何帮助,将不胜感激。

There is a SphericalUtil.computeOffset() method in Android, which returns the LatLng resulting from moving a distance from an origin in the specified heading. Android中有一个SphericalUtil.computeOffset()方法,该方法返回LatLng,该LatLng是从指定标题中的原点移动一定距离后得出的。

Sample code based on your posted code: 根据您发布的代码的示例代码:

        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))); 

在此处输入图片说明

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

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