【问题标题】:Draw five transparent circumcircles on the Google Map v2在谷歌地图 v2 上画五个透明的外接圆
【发布时间】:2013-05-21 21:00:15
【问题描述】:

我最近开始使用 Google Map v2 并发现很多事情都发生了变化。之前我使用的是 Google Map v1,所以我使用的是 MapView 的概念。

我正在尝试使用center as my current location 创建five transparent circumcircles。下面是代码,我使用Google Map v1 来绘制圆圈,它对我来说工作得很好。现在我正在尝试在Google Map v2 的下面的代码中绘制相同的圆圈。我不能在这里使用 MapView,因为我在这里使用 GoogleMap 对象。谁能帮我在 Google Map v2 上以我当前位置为中心绘制圆圈

@Override
public void onLocationChanged(Location location) {
        if (location != null) {
            GeoPoint point = new GeoPoint(
                    (int) (location.getLatitude() * 1E6),
                    (int) (location.getLongitude() * 1E6));

            // Create a LatLng object for the current location
            LatLng latLng = new LatLng(location.getLatitude(),  location.getLongitude());

            // Show the location on the Google Map
            googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

            // Zoom in the Google Map
            googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

        //   Below is the code for Google Map v1 to draw the circle on the map  

        //   if (mapOverlay == null) {
        //   mapOverlay = new MapOverlay(this, R.drawable.mark_blue);

        //   List<Overlay> listOfOverlays = mapView.getOverlays();
        //   listOfOverlays.add(mapOverlay);
        //   }

        //   mapOverlay.setPointToDraw(point);
        //   mapView.invalidate();
        }
}

一个简单的类,扩展 Overlay 并在 Google Map v1 上绘制圆圈

class MapOverlay extends Overlay {
    private GeoPoint pointToDraw;
    int[] imageNames = new int[6];

    // This is the cached Point on the screen that will get refilled on
    // every draw
    private Point mScreenPoints;

    // This is the cached decoded bitmap that will be drawn each time
    private Bitmap mBitmap;

    // Cached Paint
    private Paint mCirclePaint;

    public MapOverlay(ProximityLocationListener gpsLocationListener,
            int currentUser) {
        imageNames[0] = currentUser;
        imageNames[1] = R.drawable.tenm;
        imageNames[2] = R.drawable.twentym;
        imageNames[3] = R.drawable.thirtym;
        imageNames[4] = R.drawable.fourtym;
        imageNames[5] = R.drawable.fiftym;

        // This only needs to be made here, once. It never needs to change.
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setColor(0x10000000);
        mCirclePaint.setStyle(Style.FILL_AND_STROKE);

        // We only need to load this image once and then just keep drawing
        // it when dirtyed.
        mBitmap = BitmapFactory.decodeResource(getResources(),
                imageNames[0]);

        // This Point object will be changed every call to toPixels(), but
        // the instance can be recycled
        mScreenPoints = new Point();
    }

    public void setPointToDraw(GeoPoint point) {
        pointToDraw = point;
    }

    public GeoPoint getPointToDraw() {
        return pointToDraw;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        super.draw(canvas, mapView, shadow);
        mScreenPoints = mapView.getProjection().toPixels(pointToDraw,
                mScreenPoints);

        int totalCircle = 5;
        int radius = 40;
        int centerimagesize = 13;

        for (int i = 1; i <= totalCircle; i++) {
            canvas.drawCircle(mScreenPoints.x + 18, mScreenPoints.y + 36, i
                    * radius, mCirclePaint);
            canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),
                    imageNames[i]), ((mScreenPoints.x) + (i * radius)),
                    (mScreenPoints.y), null);
        }

        canvas.drawBitmap(mBitmap,
                (mScreenPoints.x - (centerimagesize / 2)),
                (mScreenPoints.y - (centerimagesize / 2)), null);
        super.draw(canvas, mapView, shadow);

        return true;
    }
}

我只需要在 Google Map v2 上绘制与上述代码相同的圆圈。有什么办法,我可以将上面的代码与 GoogleMap 对象一起使用,以便我可以在 Google Map v2 上绘制圆圈?

感谢您的帮助。

更新代码:-

我需要以当前位置为圆心,在 Google Maps v2 上绘制五个圆。这意味着五个圆中的每一个都具有相同的中心但具有不同的半径:第一个圆的半径为 10m,第二个圆的半径为 20m,第三个圆的半径为 30m,第四个圆的半径为 40m,第五个圆的半径为 50m。我正在使用谷歌地图 v2。

我还需要在圆的中心显示一个标记。

我正在尝试这样的方法在 Google Map v2 上画圆,但它只画了一个圆而不是五个外接圆

CircleOptions circleOptions = new CircleOptions()
                  .center(latLng)   //set center
                  .radius(500)   //set radius in meters
                  .fillColor(Color.TRANSPARENT)  //default
                  .strokeColor(0x10000000)
                  .strokeWidth(5);

                  myCircle = googleMap.addCircle(circleOptions);

我需要像这样画外接圆-

谁能帮我解决这个问题?我在 Google Map v2 中制作这个圆圈时遇到问题。任何帮助将不胜感激。

【问题讨论】:

    标签: java android google-maps-api-2


    【解决方案1】:

    只需使用googleMap.addCircle(...)在地图上添加一个圆圈

    documentation

    更新:

    我完全按照我说的去做了,我做你想做的事没有问题

    这里是代码,超级简单超级基础....

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = super.onCreateView(inflater, container, savedInstanceState);
        GoogleMap map = getMap();
    
        int radius = 500;
    
        for(int i=0; i<5; i++){
            map.addCircle(new CircleOptions().center(new LatLng(0,0)).radius(radius).fillColor(0x30000000).strokeWidth(3));
            radius += 500;
        }
        return view;
    }
    

    【讨论】:

    • @tyczi,这就是我所做的。但是我没有从上面的代码中得到相同的圆圈。我需要与从上面的代码中得到的圆圈相同。查看我用于 Google Map v2 的更新代码。
    • 好吧,v2 中没有覆盖类,所以这是在地图上放置圆圈的唯一方法。所有地图对象都是用我发布的圆圈等方法绘制的。你能说明一下有什么不同吗?
    • 好的。没关系。但是在 Google Map v2 中应该有其他方式来绘制我在 Google Map v1 上绘制的相同圆圈。使用上面更新的代码,它在地图上只画了一个圆圈。我需要有五个透明的外接圆。
    • 我用图片更新了我的问题。如果您不清楚,请告诉我。
    • 好的,我明白你现在的意思了,当你添加 5 个具有相同中心点但半径不同的圆时,你会得到第二张图片中显示的内容?因为对我来说,它们的半径看起来都是一样的
    猜你喜欢
    • 2011-10-17
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    相关资源
    最近更新 更多