简体   繁体   English

以当前位置为圆心在Google地图上绘制四个圆

[英]Draw four circle on the google map by taking current location as the center of circle

在此处输入图片说明

In my code, I am showing my current location on the Top Half of the Android Screen with an image. 在我的代码中,我正在用图像在Android屏幕的上半部分显示当前位置。 So I need to draw a circle just like above image around my current location by taking my current location as the center of the circle. 因此,我需要以当前位置为圆心,像上面的图片一样在当前位置周围绘制一个圆。 And I need to draw four circle with radius 10m, 20m, 30m, 40m. 我需要绘制四个半径为10m,20m,30m,40m的圆。 Below is my code which shows the current location on the google map. 以下是我的代码,该代码显示了Google地图上的当前位置。

    @Override
    protected void onCreate(Bundle icicle) {
        try {       
            super.onCreate(icicle);
            setContentView(R.layout.activity_main);
            initComponents();
            mapView.setBuiltInZoomControls(true);
            mapView.setSatellite(false);
            mapController = mapView.getController();
            mapController.setZoom(16);
            locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            if (locationManager == null) {
                Toast.makeText(getApplicationContext(),
                        "Location Manager Not Available", Toast.LENGTH_SHORT)
                        .show();
                return;
            }
            location = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location == null)
                location = locationManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location != null) {
                double lat = location.getLatitude();
                double lng = location.getLongitude();
                Toast.makeText(getApplicationContext(),
                        "Location Are" + lat + ":" + lng, Toast.LENGTH_SHORT)
                        .show();
                GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
                mapController.animateTo(point, new Message());
                mapOverlay.setPointToDraw(point);
                List<Overlay> listOfOverlays = mapView.getOverlays();
                listOfOverlays.clear();
                listOfOverlays.add(mapOverlay);
            }
            locationListener = new LocationListener() {
                @Override
                public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                }

                @Override
                public void onProviderEnabled(String arg0) {
                }

                @Override
                public void onProviderDisabled(String arg0) {
                }

                @Override
                public void onLocationChanged(Location l) {
                    location = l;
                    locationManager.removeUpdates(this);
                    if (l.getLatitude() == 0 || l.getLongitude() == 0) {
                    } else {
                        double lat = l.getLatitude();
                        double lng = l.getLongitude();
                        Toast.makeText(getApplicationContext(),
                                "Location Are" + lat + ":" + lng,
                                Toast.LENGTH_SHORT).show();
                    }
                }
            };
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, 1000, 10f, locationListener);
            if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener);
            }
            locationtimer = new CountDownTimer(30000, 5000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    if (location != null)
                        locationtimer.cancel();
                }

                @Override
                public void onFinish() {
                    if (location == null) {
                    }
                }
            };
            locationtimer.start();
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }

    public MapView getMapView() {
        return this.mapView;
    }

    private void initComponents() {
        mapView = (MapView) findViewById(R.id.mapView);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

The Below class is the continuation from the above. 以下类是以上内容的延续。 In the below class I am showing my current location on the google map. 在下面的课程中,我正在google地图上显示我的当前位置。 So I think I need to make changes in this class only to draw the four circle by taking current location as the center of the circle. 因此,我认为我需要在此类中进行更改,只需通过将当前位置作为圆心来绘制四个圆即可。

    class MapOverlay extends Overlay {
        private GeoPoint pointToDraw;

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

            Point screenPts = new Point();
            mapView.getProjection().toPixels(pointToDraw, screenPts);

            Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                    R.drawable.current_user);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null);
            return true;
        }
    }

}

On draw, you can simply use Canvas to draw your circles, using canvas.drawCircle(screenPts.x,screenPts.y, radius, yourPaint); 在绘制时,您可以使用canvas.drawCircle(screenPts.x,screenPts.y, radius, yourPaint);简单地使用Canvas绘制圆canvas.drawCircle(screenPts.x,screenPts.y, radius, yourPaint); As for getting your 10-20-30-40m circles, you will probably need to use a distance formula to find how many pixels equals 10, 20m, etc. Location provides a good way to do this using the distanceBetween() method. 至于获取10-20-30-40m的圆,您可能需要使用距离公式来查找有多少像素等于10、20m等。 Location提供了一种使用distanceBetween()方法的好方法。 See here for a post on that, and here for the official Android doc. 这里的上一个帖子,并在这里为Android官方文档。 Once you have a GeoPoint that is 10m away from your starting GeoPoint , you can get the screen coordinates of that GeoPoint and use that for the radius of your circle. 一旦您的GeoPoint与起始GeoPoint相距10m,就可以获取该GeoPoint的屏幕坐标,并将其用作圆的半径。 Good luck, hope this helps! 祝你好运,希望这会有帮助!

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

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